<?php

namespace app\api\model\kuyou\applet;

use app\api\controller\kuyou\applet\Commons;
use think\Model;
use think\Db;
use Exception;

class Assemble extends Model
{

    // 表名
    protected $name = 'kuyou_assemble';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 获取列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public static function getList($where,$page,$limit,$type,$sort_field,$sort_type){
        $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
        $pageStatus = true;
        if($app_page){
            if($page<0){
                $pageStatus = false;
            }
        }
        if($pageStatus){
            $list = collection(self::where(function ($query) use ($where) {
                $query->where($where);
            })->order($sort_field,$sort_type)->page($page,$limit)->field("id,purchase_code,FROM_UNIXTIME(purchase_time,'%Y-%m-%d') as purchase_time,
            business_type,money,remark,preparation_id,examine_id,examine_status,print_num,examine_time")->select())->toArray();
        }else{
            $list = collection(self::where(function ($query) use ($where) {
                $query->where($where);
            })->order($sort_field,$sort_type)->field("id,purchase_code,FROM_UNIXTIME(purchase_time,'%Y-%m-%d') as purchase_time,
            business_type,money,remark,preparation_id,examine_id,examine_status,print_num,examine_time")->select())->toArray();
        }

        $lists = [];
        foreach ($list as $list_k=>$list_v){
            $p_info = Db::name('kuyou_assemble_info')->alias('a')
                ->join('kuyou_goods g','g.id=a.goods_id')
                ->where(['a.purchase_code'=>$list_v['purchase_code'],'a.type'=>'1'])
                ->field('a.number,g.goods_name,a.goods_attr,a.unit_cost,a.cost_money,a.goods_unit')->find();
            $p_info['attr_name'] = '';
            if(!empty($p_info['goods_attr'])){
                $attribute_id = Db::name('kuyou_goods_attr')->where(['id'=>$p_info['goods_attr']])->value('attribute_id');
                $attribute_ids = Db::name('kuyou_aux_combination')->where(['id'=>$attribute_id])->value('attr_ids');
                if($attribute_ids){
                    $attribute_ids = explode('&',$attribute_ids);
                    $attribute = Db::name('kuyou_auxiliary')->where(['id'=>['in',$attribute_ids]])->column('name');
                    $p_info['attr_name'] = implode('/',$attribute);
                }
            }
            $p_info['unit_name'] = Db::name('kuyou_unit')->where(['id'=>$p_info['goods_unit']])->value('name');

            $preparation = Db::name('admin')->where(['id'=>$list_v['preparation_id']])->value('username');
            if($list_v['examine_id']){
                $examine = Db::name('admin')->where(['id'=>$list_v['examine_id']])->value('username');
                $examine_time = date('Y-m-d',$list_v['examine_time']);
            }else{
                $examine = '';
                $examine_time = '';
            }

            $info = Db::name('kuyou_assemble_info')->alias('a')
                ->join('kuyou_goods g','g.id=a.goods_id')
                ->where(['a.purchase_code'=>$list_v['purchase_code'],'a.type'=>'2'])
                ->field('a.number,g.goods_name,a.goods_attr,a.unit_cost,a.cost_money,a.goods_unit')
                ->select();
            if($type==1){
                foreach ($info as $info_k=>$info_v){
                    $row = [];
                    $row['id'] = $list_v['id'];
                    $row['preparation'] = $preparation;
                    $row['examine'] = $examine;
                    $row['examine_time'] = $examine_time;
                    $row['remark'] = $list_v['remark'];
                    $row['print_num'] = $list_v['print_num'];

                    $row['purchase_code'] = $list_v['purchase_code'];
                    $row['purchase_time'] = $list_v['purchase_time'];
                    $row['pnumber'] = $p_info['number'];
                    $row['pname'] = $p_info['goods_name'].' '.$p_info['attr_name'];
                    $row['unit_name'] = $p_info['unit_name'];
                    $row['money'] = $list_v['money'];
                    $row['punit_cost'] = $p_info['unit_cost'];
                    $row['pcost_money'] = $p_info['cost_money'];

                    $info_v['attr_name'] = '';
                    if(!empty($info_v['goods_attr'])){
                        $attribute_id = Db::name('kuyou_goods_attr')->where(['id'=>$info_v['goods_attr']])->value('attribute_id');
                        $attribute_ids = Db::name('kuyou_aux_combination')->where(['id'=>$attribute_id])->value('attr_ids');
                        if($attribute_ids){
                            $attribute_ids = explode('&',$attribute_ids);
                            $attribute = Db::name('kuyou_auxiliary')->where(['id'=>['in',$attribute_ids]])->column('name');
                            $info_v['attr_name'] = implode('/',$attribute);
                        }
                    }
                    $info_v['unit_name'] = Db::name('kuyou_unit')->where(['id'=>$info_v['goods_unit']])->value('name');

                    $row['znumber'] = $info_v['number'];
                    $row['zname'] = $info_v['goods_name'].' '.$info_v['attr_name'];
                    $row['zunit_cost'] = $info_v['unit_cost'];
                    $row['zcost_money'] = $info_v['cost_money'];
                    $row['zunit_name'] = $info_v['unit_name'];

                    $lists[] = $row;
                }
            }else{
                $row = [];
                $row['id'] = $list_v['id'];
                $row['preparation'] = $preparation;
                $row['examine'] = $examine;
                $row['examine_time'] = $examine_time;
                $row['remark'] = $list_v['remark'];
                $row['print_num'] = $list_v['print_num'];

                $row['purchase_code'] = $list_v['purchase_code'];
                $row['purchase_time'] = $list_v['purchase_time'];
                $row['pnumber'] = $p_info['number'];
                $row['pname'] = $p_info['goods_name'].' '.$p_info['attr_name'];
                $row['unit_name'] = $p_info['unit_name'];
                $row['money'] = $list_v['money'];
                $row['punit_cost'] = $p_info['unit_cost'];
                $row['pcost_money'] = $p_info['cost_money'];
                $row['zlist'] = [];

                foreach ($info as $info_k=>$info_v){
                    $rowdata = [];
                    $info_v['attr_name'] = '';
                    if(!empty($info_v['goods_attr'])){
                        $attribute_id = Db::name('kuyou_goods_attr')->where(['id'=>$info_v['goods_attr']])->value('attribute_id');
                        $attribute_ids = Db::name('kuyou_aux_combination')->where(['id'=>$attribute_id])->value('attr_ids');
                        if($attribute_ids){
                            $attribute_ids = explode('&',$attribute_ids);
                            $attribute = Db::name('kuyou_auxiliary')->where(['id'=>['in',$attribute_ids]])->column('name');
                            $info_v['attr_name'] = implode('/',$attribute);
                        }
                    }
                    $info_v['unit_name'] = Db::name('kuyou_unit')->where(['id'=>$info_v['goods_unit']])->value('name');
                    $rowdata['znumber'] = $info_v['number'];
                    $rowdata['zname'] = $info_v['goods_name'].' '.$info_v['attr_name'];
                    $rowdata['zunit_cost'] = $info_v['unit_cost'];
                    $rowdata['zcost_money'] = $info_v['cost_money'];
                    $rowdata['zunit_name'] = $info_v['unit_name'];
                    $row['zlist'][] = $rowdata;
                }
                $lists[] = $row;
            }
        }

        $count = self::where(function ($query) use ($where) {
            $query->where($where);
        })->count();
        return ['list'=>$lists,'count'=>$count];
    }

    /**
     * 获取详情
     * @param string $purchase_code
     * @return array
     */
    public static function getDetail($purchase_code){
        $detail = self::where(function ($query) use ($purchase_code) {
            $query->where('purchase_code', '=', $purchase_code);
        })->field("id,purchase_code,FROM_UNIXTIME(purchase_time,'%Y-%m-%d') as purchase_time,
            business_type,money,remark,preparation_id,examine_id,examine_status,print_num")->find();
        if($detail){
            $list = Db::name('kuyou_assemble_info')->alias('a')
                ->join('kuyou_goods g','g.id=a.goods_id')
                ->where(['a.purchase_code'=>$detail['purchase_code'],'type'=>'1'])
                ->field('a.type,a.goods_id,a.goods_code,a.goods_attr,a.goods_unit,a.stores_id,a.allow_stock,a.number,
                a.period,a.basic_number,a.unit_cost,a.cost_money,a.remark,a.batch_id,a.batch,a.guarantee,
                FROM_UNIXTIME(manufacture,\'%Y-%m-%d\') as manufacture,FROM_UNIXTIME(validity,\'%Y-%m-%d\') as validity,
                g.goods_name,g.goods_image,g.batch as batch_status,g.serial')
                ->select();
            foreach ($list as $list_k=>$list_v){
                if(!empty($list_v['period'])){
                    $list[$list_k]['period'] = unserialize($list_v['period']);
                }
            }
            $detail['p_list'] = $list;
            $zlist = Db::name('kuyou_assemble_info')->alias('a')
                ->join('kuyou_goods g','g.id=a.goods_id')
                ->where(['a.purchase_code'=>$detail['purchase_code'],'type'=>'2'])
                ->field('a.type,a.goods_id,a.goods_code,a.goods_attr,a.goods_unit,a.stores_id,a.allow_stock,a.number,
                a.period,a.basic_number,a.unit_cost,a.cost_money,a.remark,a.batch_id,a.batch,a.guarantee,
                FROM_UNIXTIME(manufacture,\'%Y-%m-%d\') as manufacture,FROM_UNIXTIME(validity,\'%Y-%m-%d\') as validity,
                g.goods_name,g.goods_image,g.batch as batch_status,g.serial')
                ->select();
            foreach ($zlist as $zlist_k=>$zlist_v){
                if(!empty($zlist_v['period'])){
                    $zlist[$zlist_k]['period'] = unserialize($zlist_v['period']);
                }
            }
            $detail['z_list'] = $zlist;
        }
        if(!empty($detail)){
            if(!empty($detail['cost_data'])){
                $detail['cost_data'] = (array)json_decode($detail['cost_data'], true);
            }
            return ['status'=>200,'msg'=>$detail];
        }else{
            return ['status'=>500,'msg'=>'未查询到相关数据'];
        }
    }

    /**
     * 添加
     * @param array $detail
     * @return array
     */
    public static function addDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'purchase_code|单据编号'=>'require',
                'purchase_time|单据时间'=>'require',
                'costs|组装费用'=>'requireNone|money',
                'plist|组合件信息'=>'require',
                'zlist|子件信息'=>'require',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $adminId = (new Commons())->getAdminId();
                if(empty($adminId)){
                    return ['status'=>410,'msg'=>'登陆无效，请重新登陆'];
                }
                $status = (new Commons())->checkCode($detail['purchase_code'],19);//验证编号是否合法
                if($status){
                    $examine= (new Commons())->getSet('examine','is_checked');
                    $serial_set= Db::name('kuyou_sets')->where(['name'=>'serial'])->value('is_checked');//序列号设置
                    $batch_set= Db::name('kuyou_sets')->where(['name'=>'batch'])->value('is_checked');//批次设置

                    $plist = (array)json_decode($detail['plist'], true);
                    $zlist = (array)json_decode($detail['zlist'], true);
                    foreach ($zlist as $zlist_k=>$zlist_v){
                        $plist[] = $zlist_v;
                    }

                    $data = [];
                    $goods_serial_all = [];//全部序列号,重复判断使用
                    $p = 2;$z = 2;
                    foreach ($plist as $k=>$v){
                        if(!empty($v['goods_code'])){//有产品编号
                            if(!isset($v['goods_unit']) || empty($v['goods_unit'])){
                                return ['status'=>500,'msg'=>'请选择单位'];
                            }
                            if(!isset($v['stores_id']) || empty($v['stores_id'])){
                                return ['status'=>500,'msg'=>'请选择仓库'];
                            }

                            $goods = Db::name('kuyou_goods')->where(['goods_code'=>$v['goods_code']])->find();
                            $goods_unit = $v['goods_unit']===''?null:(float)$v['goods_unit'];
                            $advance_rate = 1;//换算进率
                            if($goods['more_unit']==1) {
                                if($goods_unit!=$goods['unit_id']){
                                    //所选单位不等于商品默认单位，需查询进率换算基础数量
                                    $more_unit = Db::name('kuyou_unit_conversion')
                                        ->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$goods_unit])
                                        
                                        ->field('id,advance_rate')
                                        ->find();
                                    if(empty($more_unit)){
                                        return ['status'=>500,'msg'=>'商品单位信息查询失败'];
                                    }else{
                                        $advance_rate = $more_unit['advance_rate'];
                                    }
                                }
                            }
                            $basic_number = round(($advance_rate*$v['number']),4);

                            if($k==0){
                                //组合件
                                //批次
                                $batch_data = Db::name('kuyou_goods_batch')->where(['id'=>$v['batch_id']])->find();//已有批次
                                $goods_batch = [];$goods_batch_info = [];
                                if($goods['batch']==1 && $batch_set==1){
                                    if(empty($v['batch_name'])){
                                        if($batch_data){
                                            return ['status'=>500,'msg'=>'请选择生产批次'];
                                        }else{
                                            return ['status'=>500,'msg'=>'请输入生产批次'];
                                        }
                                    }
                                    if(empty($v['manufacture'])){
                                        return ['status'=>500,'msg'=>'请输入生产日期'];
                                    }
                                    if(empty($v['guarantee'])){
                                        return ['status'=>500,'msg'=>'请输入保质期'];
                                    }
                                    if(empty($v['validity'])){
                                        return ['status'=>500,'msg'=>'请输入过期时间'];
                                    }
                                    if(empty($batch_data)){//新增生产批次
                                        $goods_attr = isset($v['goods_attr'])?$v['goods_attr']:0;
                                        $batch_data = Db::name('kuyou_goods_batch')->where(['goods_code'=>$v['goods_code'],'attribute_id'=>$goods_attr,'stores_id'=>$v['stores_id']])->field('id')->find();
                                        if(empty($batch_data)){
                                            $goods_batch = [
                                                'goods_id'    => $goods['id'],
                                                'goods_code'  => $v['goods_code'],
                                                'attribute_id'=> isset($v['goods_attr'])?$v['goods_attr']:0,
                                                'stores_id'   => $v['stores_id'],
                                                'batch_name'  => $v['batch_name'],
                                                'manufacture' => strtotime($v['manufacture']),
                                                'guarantee'   => $v['guarantee'],
                                                'validity'    => strtotime($v['validity']),
                                                'createtime'  => time(),
                                                'updatetime'  => time(),
                                            ];
                                        }
                                    }
                                    $goods_batch_info = [
                                        'batch_id'    => isset($batch_data['id'])?$batch_data['id']:'',
                                        'goods_id'    => $goods['id'],
                                        'goods_code'  => $v['goods_code'],
                                        'attribute_id'=> isset($v['goods_attr'])?$v['goods_attr']:0,
                                        'goods_unit'  => (int)$v['goods_unit'],
                                        'stores_id'   => $v['stores_id'],
                                        'stock'       => (float)$v['number'],
                                        'number'      => (float)$v['number'],
                                        'batch_name'  => isset($batch_data['batch'])?$batch_data['batch']:'',
                                        'manufacture' => strtotime($v['manufacture']),
                                        'guarantee'   => $v['guarantee'],
                                        'validity'    => strtotime($v['validity']),
                                        'status'      => '0',
                                        'type'        => 2,
                                        'createtime'  => time(),
                                        'updatetime'  => time(),
                                    ];
                                }
                                //序列号
                                $goods_serial='';
                                if(isset($v['period_period']) && $goods['serial']==1 && $serial_set==1){
                                    if(empty($v['period_period'])){
                                        return ['status'=>500,'msg'=>'请输入商品序列号'];
                                    }
                                    $goods_serial = serialize($v['period_period']);
                                    $period_period = $v['period_period'];
                                    if(!is_array($period_period)){
                                        $period_period = (array)json_decode($period_period, true);
                                    }
                                    $serial_code = [];
                                    foreach ($period_period as $period_period_k=>$period_period_v){
                                        if(in_array($period_period_v['serial'],$goods_serial_all)){
                                            return ['status'=>500,'msg'=>'商品序列号'.$period_period_v['serial'].'重复'];
                                        }else{
                                            $goods_serial_all[] = $period_period_v['serial'];
                                        }
                                        $serial_code[] = $period_period_v['serial'];
                                    }
                                    $serial = Db::name('kuyou_goods_serial')->where(['serial'=>['in',$serial_code]])->column('serial');
                                    if($serial){
                                        return ['status'=>500,'msg'=>'商品序列号'.$serial[0].'已占用'];
                                    }
                                    $basic_number = (float)$v['number'];
                                }

                                $data[] = [
                                    'purchase_code'     =>  $detail['purchase_code'],
                                    'purchase_time'     =>  strtotime($detail['purchase_time']),
                                    'business_type'     =>  1,
                                    'type'              =>  1,

                                    'goods_id'          =>  $goods['id'],//商品信息
                                    'goods_code'        =>  $goods['goods_code'],
                                    'goods_attr'        =>  $v['goods_attr']===''?null:(float)$v['goods_attr'],
                                    'goods_unit'        =>  $v['goods_unit']===''?null:(float)$v['goods_unit'],

                                    'stores_id'         =>  $v['stores_id']===''?null:(float)$v['stores_id'],
                                    'allow_stock'       =>  $v['allow_stock']===''?null:(float)$v['allow_stock'],
                                    'number'            =>  $v['number']===''?null:(float)$v['number'],
                                    'period'            =>  $goods_serial,
                                    'basic_number'      =>  $basic_number,
                                    'unit_cost'         =>  $v['unit_cost']===''?null:(float)$v['unit_cost'],
                                    'cost_money'        =>  $v['cost_money']===''?null:(float)$v['cost_money'],
                                    'remark'            =>  $v['remark']===''?null:$v['remark'],

                                    'batch'             =>  $v['batch_name']===''?null:$v['batch_name'],
                                    'batch_id'          =>  $v['batch_id']===''?null:(float)$v['batch_id'],
                                    'manufacture'       =>  $v['manufacture']===''?null:strtotime($v['manufacture']),
                                    'guarantee'         =>  $v['guarantee']===''?null:(float)$v['guarantee'],
                                    'validity'          =>  $v['validity']===''?null:strtotime($v['validity']),

                                    'preparation_id'    =>  $adminId,
                                    'operation_id'      =>  $adminId,

                                    'createtime'        => time(),
                                    'updatetime'        => time(),
                                    'goods_batch'       => $goods_batch,
                                    'goods_batch_info'  => $goods_batch_info
                                ];
                                $p = 1;
                            }else{
                                //子件
                                if($v==$plist[0]['goods_code']){
                                    return ['status'=>500,'msg'=>'子件不能和组合件为同一商品'];
                                }
                                //批次
                                $batch_data = Db::name('kuyou_goods_batch')->where(['id'=>$v['batch_id']])->find();//已有批次
                                $goods_batch = [];$goods_batch_info = [];
                                if($goods['batch']==1 && $batch_set==1){
                                    if(empty($v['batch_name'])){
                                        return ['status'=>500,'msg'=>'请选择生产批次'];
                                    }
                                    if(empty($v['manufacture'])){
                                        return ['status'=>500,'msg'=>'请输入生产日期'];
                                    }
                                    if(empty($v['guarantee'])){
                                        return ['status'=>500,'msg'=>'请输入保质期'];
                                    }
                                    if(empty($v['validity'])){
                                        return ['status'=>500,'msg'=>'请输入过期时间'];
                                    }
                                    if(empty($batch_data)){
                                        return ['status'=>500,'msg'=>'商品批次信息查询失败'];
                                    }
                                    $goods_batch_info = [
                                        'batch_id'    => isset($batch_data['id'])?$batch_data['id']:'',
                                        'goods_id'    => $goods['id'],
                                        'goods_code'  => $v['goods_code'],
                                        'attribute_id'=> isset($v['goods_attr'])?$v['goods_attr']:0,
                                        'goods_unit'  => (int)$v['goods_unit'],
                                        'stores_id'   => $v['stores_id'],
                                        'stock'       => (float)$v['number'],
                                        'number'      => (float)$v['number'],
                                        'batch_name'  => isset($batch_data['batch'])?$batch_data['batch']:'',
                                        'manufacture' => strtotime($v['manufacture']),
                                        'guarantee'   => $v['guarantee'],
                                        'validity'    => strtotime($v['validity']),
                                        'status'      => '0',
                                        'type'        => 2,
                                        'createtime'  => time(),
                                        'updatetime'  => time(),
                                    ];
                                }
                                //序列号
                                $goods_serial='';
                                if(isset($v['period_period']) && $goods['serial']==1 && $serial_set==1){
                                    if(empty($v['period_period'])){
                                        return ['status'=>500,'msg'=>'请输入商品序列号'];
                                    }
                                    $goods_serial = serialize($v['period_period']);
                                    $period_period = $v['period_period'];
                                    if(!is_array($period_period)){
                                        $period_period = (array)json_decode($period_period, true);
                                    }
                                    $serial_code = [];
                                    foreach ($period_period as $period_period_k=>$period_period_v){
                                        if(in_array($period_period_v['serial'],$goods_serial_all)){
                                            return ['status'=>500,'msg'=>'商品序列号'.$period_period_v['serial'].'重复'];
                                        }else{
                                            $goods_serial_all[] = $period_period_v['serial'];
                                        }
                                        $serial_code[] = $period_period_v['serial'];
                                    }
                                    $serial = Db::name('kuyou_goods_serial')->where(['serial'=>['in',$serial_code],'store_status'=>'1'])->column('serial');
                                    if(sizeof($serial)!=sizeof($serial_code)){
                                        foreach ($serial_code as $serial_code_k=>$serial_code_v){
                                            if(in_array($serial_code_v,$serial)){
                                                return ['status'=>500,'msg'=>'商品序列号'.$serial_code_v.'不在库，不可出库'];
                                            }
                                        }
                                        return ['status'=>500,'msg'=>'商品序列号不在库，不可出库'];
                                    }
                                    $basic_number = (float)$v['number'];
                                }

                                $data[] = [
                                    'purchase_code'     =>  $detail['purchase_code'],
                                    'purchase_time'     =>  strtotime($detail['purchase_time']),
                                    'business_type'     =>  1,
                                    'type'              =>  2,

                                    'goods_id'          =>  $goods['id'],//商品信息
                                    'goods_code'        =>  $goods['goods_code'],
                                    'goods_attr'        =>  $v['goods_attr']===''?null:(float)$v['goods_attr'],
                                    'goods_unit'        =>  $v['goods_unit']===''?null:(float)$v['goods_unit'],

                                    'stores_id'         =>  $v['stores_id']===''?null:(float)$v['stores_id'],
                                    'allow_stock'       =>  $v['allow_stock']===''?null:(float)$v['allow_stock'],
                                    'number'            =>  $v['number']===''?null:(float)$v['number'],
                                    'period'            =>  $goods_serial,
                                    'basic_number'      =>  $basic_number,
                                    'unit_cost'         =>  $v['unit_cost']===''?null:(float)$v['unit_cost'],
                                    'cost_money'        =>  $v['cost_money']===''?null:(float)$v['cost_money'],
                                    'remark'            =>  $v['remark']===''?null:$v['remark'],

                                    'batch'             =>  $v['batch_name']===''?null:$v['batch_name'],
                                    'batch_id'          =>  $v['batch_id']===''?null:(float)$v['batch_id'],
                                    'manufacture'       =>  $v['manufacture']===''?null:strtotime($v['manufacture']),
                                    'guarantee'         =>  $v['guarantee']===''?null:(float)$v['guarantee'],
                                    'validity'          =>  $v['validity']===''?null:strtotime($v['validity']),

                                    'preparation_id'    =>  $adminId,
                                    'operation_id'      =>  $adminId,

                                    'createtime'        => time(),
                                    'updatetime'        => time(),
                                    'goods_batch'       => $goods_batch,
                                    'goods_batch_info'  => $goods_batch_info
                                ];
                                $z = 1;
                            }
                        }
                    }
                    if($p==2){
                        return ['status'=>500,'msg'=>'组合件信息不能为空'];
                    }
                    if($z==2){
                        return ['status'=>500,'msg'=>'组合子件信息不能为空'];
                    }
                    if(!empty($data)){
                        $rowdata = [
                            'purchase_code'     =>  $detail['purchase_code'],
                            'purchase_time'     =>  strtotime($detail['purchase_time']),
                            'business_type'     =>  1,
                            'money'             =>  $detail['costs']===''?null:(float)$detail['costs'],
                            'remark'            =>  $detail['note']===''?null:$detail['note'],
                            'preparation_id'    =>  $adminId,
                            'operation_id'      =>  $adminId,
                            'createtime'        => time(),
                            'updatetime'        => time(),
                        ];
                    }else{
                        return ['status'=>500,'msg'=>'组件信息不能为空'];
                    }

                    $model = new Assemble();
                    $row = $model->where(['purchase_code'=>$detail['purchase_code']])->value('id');
                    if($row){
                        return ['status'=>500,'msg'=>'单据编号已使用，请重新输入单据编号'];
                    }
                    $result = false;
                    Db::startTrans();
                    try {
                        $model->allowField(true)->save($rowdata);
                        $result = $model->id;

                        foreach ($data as $data_k=>$data_v){
                            $goods_batch = $data_v['goods_batch'];
                            unset($data_v['goods_batch']);
                            $goods_batch_info = $data_v['goods_batch_info'];
                            unset($data_v['goods_batch_info']);
                            $goods_batch_id = 0;
                            if(!empty($goods_batch)){
                                $goods_batch_id = Db::name('kuyou_goods_batch')->insertGetId($goods_batch);
                            }
                            if($goods_batch_id>0){
                                $goods_batch_info['batch_id'] = $goods_batch_id;
                                $goods_batch_info['batch_name'] = $goods_batch['batch_name'];

                                $data_v['batch_id'] = $goods_batch_id;
                                $data_v['batch'] = $goods_batch['batch_name'];
                            }
                            $purchase_id = Db::name('kuyou_assemble_info')->insertGetId($data_v);
                            //批次
                            if($goods_batch_info){
                                $goods_batch_info['assemble_id'] = $purchase_id;
                                Db::name('kuyou_goods_batch_info')->insert($goods_batch_info);
                            }
                        }
                        Db::commit();
                    } catch (Exception $e) {
                        Db::rollback();
                        return ['status'=>500,'msg'=>$e->getMessage()];
                    }
                    if($result){
                        if($examine!=1){
                            try {
                                $exa = self::examine($detail['purchase_code']);
                            } catch (Exception $e) {
                            }
                            if(!isset($exa['status']) || (isset($exa['status']) && $exa['status']!=1)){
                                try {
                                    self::delDetail($detail['purchase_code']);
                                } catch (Exception $e) {
                                }
                                return ['status'=>500,'msg'=>$exa['msg']];
                            }
                        }
                        return ['status'=>200,'msg'=>$detail['purchase_code']];
                    }else{
                        return ['status'=>500,'msg'=>'添加失败'];
                    }
                }else{
                    return ['status'=>500,'msg'=>'编号规则错误'];
                }
            }
        }
    }

    /**
     * 更新
     * @param array $detail
     * @return array
     */
    public static function editDetail($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'old_purchase_code|原单据编号'=>'require',
                'purchase_code|单据编号'=>'require',
                'purchase_time|单据时间'=>'require',
                'costs|组装费用'=>'requireNone|money',
                'plist|组合件信息'=>'require',
                'zlist|子件信息'=>'require',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $adminId = (new Commons())->getAdminId();
                if(empty($adminId)){
                    return ['status'=>410,'msg'=>'登陆无效，请重新登陆'];
                }
                $status = (new Commons())->checkCode($detail['purchase_code'],19);//验证编号是否合法
                if($status){
                    $model = new Assemble();
                    $row = $model->where(['purchase_code'=>$detail['old_purchase_code']])->find();
                    if(empty($row)){
                        return ['status'=>500,'msg'=>'修改的单据不存在'];
                    }
                    $old_purchase_code = $detail['old_purchase_code'];

                    $examine= (new Commons())->getSet('examine','is_checked');
                    $serial_set= Db::name('kuyou_sets')->where(['name'=>'serial'])->value('is_checked');//序列号设置
                    $batch_set= Db::name('kuyou_sets')->where(['name'=>'batch'])->value('is_checked');//批次设置

                    $plist = (array)json_decode($detail['plist'], true);
                    $zlist = (array)json_decode($detail['zlist'], true);
                    foreach ($zlist as $zlist_k=>$zlist_v){
                        $plist[] = $zlist_v;
                    }

                    $data = [];
                    $goods_serial_all = [];//全部序列号,重复判断使用
                    $p = 2;$z = 2;
                    foreach ($plist as $k=>$v){
                        if(!empty($v['goods_code'])){//有产品编号
                            if(!isset($v['goods_unit']) || empty($v['goods_unit'])){
                                return ['status'=>500,'msg'=>'请选择单位'];
                            }
                            if(!isset($v['stores_id']) || empty($v['stores_id'])){
                                return ['status'=>500,'msg'=>'请选择仓库'];
                            }

                            $goods = Db::name('kuyou_goods')->where(['goods_code'=>$v['goods_code']])->find();
                            $goods_unit = $v['goods_unit']===''?null:(float)$v['goods_unit'];
                            $advance_rate = 1;//换算进率
                            if($goods['more_unit']==1) {
                                if($goods_unit!=$goods['unit_id']){
                                    //所选单位不等于商品默认单位，需查询进率换算基础数量
                                    $more_unit = Db::name('kuyou_unit_conversion')
                                        ->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$goods_unit])
                                        
                                        ->field('id,advance_rate')
                                        ->find();
                                    if(empty($more_unit)){
                                        return ['status'=>500,'msg'=>'商品单位信息查询失败'];
                                    }else{
                                        $advance_rate = $more_unit['advance_rate'];
                                    }
                                }
                            }
                            $basic_number = round(($advance_rate*$v['number']),4);

                            if($k==0){
                                //组合件
                                //批次
                                $batch_data = Db::name('kuyou_goods_batch')->where(['id'=>$v['batch_id']])->find();//已有批次
                                $goods_batch = [];$goods_batch_info = [];
                                if($goods['batch']==1 && $batch_set==1){
                                    if(empty($v['batch_name'])){
                                        if($batch_data){
                                            return ['status'=>500,'msg'=>'请选择生产批次'];
                                        }else{
                                            return ['status'=>500,'msg'=>'请输入生产批次'];
                                        }
                                    }
                                    if(empty($v['manufacture'])){
                                        return ['status'=>500,'msg'=>'请输入生产日期'];
                                    }
                                    if(empty($v['guarantee'])){
                                        return ['status'=>500,'msg'=>'请输入保质期'];
                                    }
                                    if(empty($v['validity'])){
                                        return ['status'=>500,'msg'=>'请输入过期时间'];
                                    }
                                    if(empty($batch_data)){//新增生产批次
                                        $goods_attr = isset($v['goods_attr'])?$v['goods_attr']:0;
                                        $batch_data = Db::name('kuyou_goods_batch')->where(['goods_code'=>$v['goods_code'],'attribute_id'=>$goods_attr,'stores_id'=>$v['stores_id']])->field('id')->find();
                                        if(empty($batch_data)){
                                            $goods_batch = [
                                                'goods_id'    => $goods['id'],
                                                'goods_code'  => $v['goods_code'],
                                                'attribute_id'=> isset($v['goods_attr'])?$v['goods_attr']:0,
                                                'stores_id'   => $v['stores_id'],
                                                'batch_name'  => $v['batch_name'],
                                                'manufacture' => strtotime($v['manufacture']),
                                                'guarantee'   => $v['guarantee'],
                                                'validity'    => strtotime($v['validity']),
                                                'createtime'  => time(),
                                                'updatetime'  => time(),
                                            ];
                                        }
                                    }
                                    $goods_batch_info = [
                                        'batch_id'    => isset($batch_data['id'])?$batch_data['id']:'',
                                        'goods_id'    => $goods['id'],
                                        'goods_code'  => $v['goods_code'],
                                        'attribute_id'=> isset($v['goods_attr'])?$v['goods_attr']:0,
                                        'goods_unit'  => (int)$v['goods_unit'],
                                        'stores_id'   => $v['stores_id'],
                                        'stock'       => (float)$v['number'],
                                        'number'      => (float)$v['number'],
                                        'batch_name'  => isset($batch_data['batch'])?$batch_data['batch']:'',
                                        'manufacture' => strtotime($v['manufacture']),
                                        'guarantee'   => $v['guarantee'],
                                        'validity'    => strtotime($v['validity']),
                                        'status'      => '0',
                                        'type'        => 2,
                                        'createtime'  => time(),
                                        'updatetime'  => time(),
                                    ];
                                }
                                //序列号
                                $goods_serial='';
                                if(isset($v['period_period']) && $goods['serial']==1 && $serial_set==1){
                                    if(empty($v['period_period'])){
                                        return ['status'=>500,'msg'=>'请输入商品序列号'];
                                    }
                                    $goods_serial = serialize($v['period_period']);
                                    $period_period = $v['period_period'];
                                    if(!is_array($period_period)){
                                        $period_period = (array)json_decode($period_period, true);
                                    }
                                    $serial_code = [];
                                    foreach ($period_period as $period_period_k=>$period_period_v){
                                        if(in_array($period_period_v['serial'],$goods_serial_all)){
                                            return ['status'=>500,'msg'=>'商品序列号'.$period_period_v['serial'].'重复'];
                                        }else{
                                            $goods_serial_all[] = $period_period_v['serial'];
                                        }
                                        $serial_code[] = $period_period_v['serial'];
                                    }
                                    $serial = Db::name('kuyou_goods_serial')->where(['serial'=>['in',$serial_code]])->column('serial');
                                    if($serial){
                                        return ['status'=>500,'msg'=>'商品序列号'.$serial[0].'已占用'];
                                    }
                                    $basic_number = (float)$v['number'];
                                }

                                $data[] = [
                                    'purchase_code'     =>  $detail['purchase_code'],
                                    'purchase_time'     =>  strtotime($detail['purchase_time']),
                                    'business_type'     =>  1,
                                    'type'              =>  1,

                                    'goods_id'          =>  $goods['id'],//商品信息
                                    'goods_code'        =>  $goods['goods_code'],
                                    'goods_attr'        =>  $v['goods_attr']===''?null:(float)$v['goods_attr'],
                                    'goods_unit'        =>  $v['goods_unit']===''?null:(float)$v['goods_unit'],

                                    'stores_id'         =>  $v['stores_id']===''?null:(float)$v['stores_id'],
                                    'allow_stock'       =>  $v['allow_stock']===''?null:(float)$v['allow_stock'],
                                    'number'            =>  $v['number']===''?null:(float)$v['number'],
                                    'period'            =>  $goods_serial,
                                    'basic_number'      =>  $basic_number,
                                    'unit_cost'         =>  $v['unit_cost']===''?null:(float)$v['unit_cost'],
                                    'cost_money'        =>  $v['cost_money']===''?null:(float)$v['cost_money'],
                                    'remark'            =>  $v['remark']===''?null:$v['remark'],

                                    'batch'             =>  $v['batch_name']===''?null:$v['batch_name'],
                                    'batch_id'          =>  $v['batch_id']===''?null:(float)$v['batch_id'],
                                    'manufacture'       =>  $v['manufacture']===''?null:strtotime($v['manufacture']),
                                    'guarantee'         =>  $v['guarantee']===''?null:(float)$v['guarantee'],
                                    'validity'          =>  $v['validity']===''?null:strtotime($v['validity']),

                                    'preparation_id'    =>  $adminId,
                                    'operation_id'      =>  $adminId,

                                    'createtime'        => time(),
                                    'updatetime'        => time(),
                                    'goods_batch'       => $goods_batch,
                                    'goods_batch_info'  => $goods_batch_info
                                ];
                                $p = 1;
                            }else{
                                //子件
                                if($v==$plist[0]['goods_code']){
                                    return ['status'=>500,'msg'=>'子件不能和组合件为同一商品'];
                                }
                                //批次
                                $batch_data = Db::name('kuyou_goods_batch')->where(['id'=>$v['batch_id']])->find();//已有批次
                                $goods_batch = [];$goods_batch_info = [];
                                if($goods['batch']==1 && $batch_set==1){
                                    if(empty($v['batch_name'])){
                                        return ['status'=>500,'msg'=>'请选择生产批次'];
                                    }
                                    if(empty($v['manufacture'])){
                                        return ['status'=>500,'msg'=>'请输入生产日期'];
                                    }
                                    if(empty($v['guarantee'])){
                                        return ['status'=>500,'msg'=>'请输入保质期'];
                                    }
                                    if(empty($v['validity'])){
                                        return ['status'=>500,'msg'=>'请输入过期时间'];
                                    }
                                    if(empty($batch_data)){
                                        return ['status'=>500,'msg'=>'商品批次信息查询失败'];
                                    }
                                    $goods_batch_info = [
                                        'batch_id'    => isset($batch_data['id'])?$batch_data['id']:'',
                                        'goods_id'    => $goods['id'],
                                        'goods_code'  => $v['goods_code'],
                                        'attribute_id'=> isset($v['goods_attr'])?$v['goods_attr']:0,
                                        'goods_unit'  => (int)$v['goods_unit'],
                                        'stores_id'   => $v['stores_id'],
                                        'stock'       => (float)$v['number'],
                                        'number'      => (float)$v['number'],
                                        'batch_name'  => isset($batch_data['batch'])?$batch_data['batch']:'',
                                        'manufacture' => strtotime($v['manufacture']),
                                        'guarantee'   => $v['guarantee'],
                                        'validity'    => strtotime($v['validity']),
                                        'status'      => '0',
                                        'type'        => 2,
                                        'createtime'  => time(),
                                        'updatetime'  => time(),
                                    ];
                                }
                                //序列号
                                $goods_serial='';
                                if(isset($v['period_period']) && $goods['serial']==1 && $serial_set==1){
                                    if(empty($v['period_period'])){
                                        return ['status'=>500,'msg'=>'请输入商品序列号'];
                                    }
                                    $goods_serial = serialize($v['period_period']);
                                    $period_period = $v['period_period'];
                                    if(!is_array($period_period)){
                                        $period_period = (array)json_decode($period_period, true);
                                    }
                                    $serial_code = [];
                                    foreach ($period_period as $period_period_k=>$period_period_v){
                                        if(in_array($period_period_v['serial'],$goods_serial_all)){
                                            return ['status'=>500,'msg'=>'商品序列号'.$period_period_v['serial'].'重复'];
                                        }else{
                                            $goods_serial_all[] = $period_period_v['serial'];
                                        }
                                        $serial_code[] = $period_period_v['serial'];
                                    }
                                    $serial = Db::name('kuyou_goods_serial')->where(['serial'=>['in',$serial_code],'store_status'=>'1'])->column('serial');
                                    if(sizeof($serial)!=sizeof($serial_code)){
                                        foreach ($serial_code as $serial_code_k=>$serial_code_v){
                                            if(in_array($serial_code_v,$serial)){
                                                return ['status'=>500,'msg'=>'商品序列号'.$serial_code_v.'不在库，不可出库'];
                                            }
                                        }
                                        return ['status'=>500,'msg'=>'商品序列号不在库，不可出库'];
                                    }
                                    $basic_number = (float)$v['number'];
                                }

                                $data[] = [
                                    'purchase_code'     =>  $detail['purchase_code'],
                                    'purchase_time'     =>  strtotime($detail['purchase_time']),
                                    'business_type'     =>  1,
                                    'type'              =>  2,

                                    'goods_id'          =>  $goods['id'],//商品信息
                                    'goods_code'        =>  $goods['goods_code'],
                                    'goods_attr'        =>  $v['goods_attr']===''?null:(float)$v['goods_attr'],
                                    'goods_unit'        =>  $v['goods_unit']===''?null:(float)$v['goods_unit'],

                                    'stores_id'         =>  $v['stores_id']===''?null:(float)$v['stores_id'],
                                    'allow_stock'       =>  $v['allow_stock']===''?null:(float)$v['allow_stock'],
                                    'number'            =>  $v['number']===''?null:(float)$v['number'],
                                    'period'            =>  $goods_serial,
                                    'basic_number'      =>  $basic_number,
                                    'unit_cost'         =>  $v['unit_cost']===''?null:(float)$v['unit_cost'],
                                    'cost_money'        =>  $v['cost_money']===''?null:(float)$v['cost_money'],
                                    'remark'            =>  $v['remark']===''?null:$v['remark'],

                                    'batch'             =>  $v['batch_name']===''?null:$v['batch_name'],
                                    'batch_id'          =>  $v['batch_id']===''?null:(float)$v['batch_id'],
                                    'manufacture'       =>  $v['manufacture']===''?null:strtotime($v['manufacture']),
                                    'guarantee'         =>  $v['guarantee']===''?null:(float)$v['guarantee'],
                                    'validity'          =>  $v['validity']===''?null:strtotime($v['validity']),

                                    'preparation_id'    =>  $adminId,
                                    'operation_id'      =>  $adminId,

                                    'createtime'        => time(),
                                    'updatetime'        => time(),
                                    'goods_batch'       => $goods_batch,
                                    'goods_batch_info'  => $goods_batch_info
                                ];
                                $z = 1;
                            }
                        }
                    }
                    if($p==2){
                        return ['status'=>500,'msg'=>'组合件信息不能为空'];
                    }
                    if($z==2){
                        return ['status'=>500,'msg'=>'组合子件信息不能为空'];
                    }
                    if(!empty($data)){
                        $rowdata = [
                            'purchase_code'     =>  $detail['purchase_code'],
                            'purchase_time'     =>  strtotime($detail['purchase_time']),
                            'business_type'     =>  1,
                            'money'             =>  $detail['costs']===''?null:(float)$detail['costs'],
                            'remark'            =>  $detail['note']===''?null:$detail['note'],
                            'preparation_id'    =>  $adminId,
                            'operation_id'      =>  $adminId,
                            'updatetime'        => time(),
                        ];
                    }else{
                        return ['status'=>500,'msg'=>'组件信息不能为空'];
                    }

                    if($detail['purchase_code']!=$detail['old_purchase_code']){
                        $purchase_id = $model->where(['purchase_code'=>$detail['purchase_code']])->value('id');
                        if($purchase_id){
                            return ['status'=>500,'msg'=>'单据编号已使用，请重新输入单据编号'];
                        }
                    }
                    $goods_purchase_info = Db::name('kuyou_assemble_info')->where(['purchase_code'=>$old_purchase_code])->column('id');

                    if($row){
                        $res = false;
                        Db::startTrans();
                        try {
                            //关闭审核功能需先反审核一次已审核数据
                            if($examine!=1 && $row['examine_status']==1){
                                self::examinef($detail['old_purchase_code']);
                            }
                            $res = $model->where(['purchase_code'=>$detail['old_purchase_code']])->update($rowdata);
                            Db::name('kuyou_assemble_info')->where(['purchase_code'=>$old_purchase_code])->delete();
                            Db::name('kuyou_goods_batch_info')->where(['assemble_id'=>['in',$goods_purchase_info]])->delete();
                            foreach ($data as $data_k=>$data_v){
                                $goods_batch = $data_v['goods_batch'];
                                unset($data_v['goods_batch']);
                                $goods_batch_info = $data_v['goods_batch_info'];
                                unset($data_v['goods_batch_info']);
                                $goods_batch_id = 0;
                                if(!empty($goods_batch)){
                                    $goods_batch_id = Db::name('kuyou_goods_batch')->insertGetId($goods_batch);
                                }
                                if($goods_batch_id>0){
                                    $goods_batch_info['batch_id'] = $goods_batch_id;
                                    $goods_batch_info['batch_name'] = $goods_batch['batch_name'];

                                    $data_v['batch_id'] = $goods_batch_id;
                                    $data_v['batch'] = $goods_batch['batch_name'];
                                }
                                $purchase_id = Db::name('kuyou_assemble_info')->insertGetId($data_v);

                                //批次
                                if($goods_batch_info){
                                    $goods_batch_info['assemble_id'] = $purchase_id;
                                    Db::name('kuyou_goods_batch_info')->insert($goods_batch_info);
                                }
                            }
                            Db::commit();
                        } catch (Exception $e) {
                            Db::rollback();
                            return ['status'=>500,'msg'=>$e->getMessage()];
                        }
                        if($res){
                            if($examine!=1){
                                try {
                                    $exa = self::examine($detail['purchase_code']);
                                } catch (Exception $e) {
                                }
                                if(!isset($exa['status']) || (isset($exa['status']) && $exa['status']!=1)){
                                    return ['status'=>500,'msg'=>$exa['msg']];
                                }
                            }
                            return ['status'=>200,'msg'=>'更新成功'];
                        }else{
                            return ['status'=>500,'msg'=>'更新失败'];
                        }
                    }else{
                        return ['status'=>500,'msg'=>'未查询到可更新数据'];
                    }
                }else{
                    return ['status'=>500,'msg'=>'编号规则错误'];
                }
            }
        }
    }

    /**
     * 删除
     * @param string $purchase_code
     * @return array
     */
    public static function delDetail($purchase_code){
        if(empty($purchase_code)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $purchase_code = explode(',',$purchase_code);
            $model = new Assemble();
            $examine= (new Commons())->getSet('examine','is_checked');

            $list = $model->where(['purchase_code'=>['in',$purchase_code]])->select();
            $goods_purchase_code = [];
            foreach ($list as $list_k=>$list_v){
                if($list_v['examine_status']==1 && $examine==1){
                    return ['status'=>500,'msg'=>'不能删除已审核的数据'];
                }
                if(!in_array($list_v['purchase_code'],$goods_purchase_code)){
                    $goods_purchase_code[] = $list_v['purchase_code'];
                }
            }
            $goods_purchase_info = Db::name('kuyou_assemble_info')->where(['purchase_code'=>['in',$goods_purchase_code]])->column('id');
            $count = 0;
            $res = false;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    if ($v['examine_status']==1 && $examine!=1){
                        try {
                            self::examinef($v['purchase_code']);
                        } catch (Exception $e) {
                        }
                    }
                    $res = $model->where(['purchase_code'=>$v['purchase_code']])->delete();
                    $count += $res;
                    Db::name('kuyou_assemble_info')->where(['purchase_code'=>$v['purchase_code']])->delete();
                }
                Db::name('kuyou_goods_batch_info')->where(['assemble_id'=>['in',$goods_purchase_info]])->delete();
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                return ['status'=>500,'msg'=>$e->getMessage()];
            }
            if($res){
                return ['status'=>200,'msg'=>$count];
            }else{
                return ['status'=>500,'msg'=>'删除失败'];
            }
        }
    }

    /**
     * 审核
     * @param array $purchase_code
     * @return array
     */
    public static function examine($purchase_code = null){
        if(empty($purchase_code)){
            return ['status'=>400,'msg'=>'请选择要操作的数据'];
        }
        $adminId = (new Commons())->getAdminId();
        if(empty($adminId)){
            return ['status'=>410,'msg'=>'登陆无效，请重新登陆'];
        }
        $model = new Assemble();
        $list = $model->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'0'])->select();
        if(empty($list)){
            return ['status'=>500,'msg'=>'请选择需要审核的数据'];
        }
        $serial_set= Db::name('kuyou_sets')->where(['name'=>'serial'])->value('is_checked');//序列号设置
        $batch_set= Db::name('kuyou_sets')->where(['name'=>'batch'])->value('is_checked');//批次设置

        $purchase_code = $model->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'0'])->column('purchase_code');

        $editstock = [];//库存表修改库存
        $addstock = [];//库存表添加库存
        $goods_stock = [];//商品列表库存
        $goods_stock_log = [];//商品收发明细
        $goods_batch_add = [];//批次库存新增
        $goods_batch_edit = [];//批次库存更新
        $goods_batch_log = [];//批次记录
        $per_code = [];//序列号
        $goods_serial = [];//序列号添加
        $edit_serial = [];//序列号更新
        $serial_log = [];//序列号跟踪
        $info_list = Db::name('kuyou_assemble_info')->where(['purchase_code'=>['in',$purchase_code]])->select();
        foreach ($info_list as $info_list_k=>$info_list_v){
            $goods = Db::name('kuyou_goods')->where(['id'=>$info_list_v['goods_id']])->find();
            $stock_number = $info_list_v['basic_number'];//基础数量
            if($info_list_v['type']==1){//组合件
                //商品表库存
                $status = false;
                foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){
                    if($goods_stock_v['id']==$goods['id']){
                        $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory']+$stock_number;
                        $status = true;
                    }
                }
                if(!$status){
                    $goods_stock[] = [
                        'id'=>$goods['id'],
                        'inventory'=>$goods['inventory']+$stock_number,
                    ];
                }
                //商品表库存结束

                //商品库存表库存
                $where = [];
                $where['goods_code'] = $info_list_v['goods_code'];
                $where['stores_id'] = $info_list_v['stores_id'];
                $goods_attr = 0;
                if($info_list_v['goods_attr']>0){
                    $goods_attr = Db::name('kuyou_goods_attr')->where(['id'=>$info_list_v['goods_attr']])->value('attribute_id');
                    if($goods_attr>0){
                        $where['attribute_id'] = $goods_attr;
                    }
                }
                $stock = Db::name('kuyou_goods_stock')->where($where)->field('stock,id,cost_money')->find();
                if($stock){
                    $status = false;
                    foreach ($editstock as $editstock_k=>$editstock_v){
                        if($editstock_v['id']==$stock['id']){
                            $editstock[$editstock_k]['stock'] += $stock_number;
                            $editstock[$editstock_k]['cost_money'] = (round($editstock_v['cost_money']+$info_list_v['cost_money'],4));
                            $status = true;
                        }
                    }
                    if(!$status){
                        $editstock[] = [
                            'id'=>$stock['id'],
                            'stock'=>(round($stock['stock']+$stock_number,4)),
                            'cost_money'=>(round($stock['cost_money']+$info_list_v['cost_money'],4))
                        ];
                    }
                }else{
                    $addstock[] = [
                        'goods_id'  => isset($goods['id'])?$goods['id']:'',
                        'goods_code'=> $info_list_v['goods_code'],
                        'attribute_id'  => isset($goods_attr)?$goods_attr:0,
                        'stores_id'  => $info_list_v['stores_id'],
                        'stock'  => $stock_number,
                        'cost_money'=>(round($info_list_v['cost_money'],4)),
                        'createtime'  => time(),
                        'updatetime'  => time(),
                    ];
                }
                //商品库存表库存结束

                //收发详情
                $goods_stock_log[] = [
                    'purchase_code' => $info_list_v['purchase_code'],
                    'purchase_id'   => $info_list_v['id'],
                    'purchase_time' => $info_list_v['purchase_time'],
                    'business_type' => '12',
                    'goods_id'      => $goods['id'],
                    'goods_code'    => $goods['goods_code'],
                    'attribute_id'  => (float)$info_list_v['goods_attr'],
                    'goods_unit'    => (float)$info_list_v['goods_unit'],
                    'stores_id'     => (float)$info_list_v['stores_id'],
                    'remark'        => $info_list_v['remark'],
                    'enter_number'  => (float)$info_list_v['number'],
                    'enter_basics'  => (float)$stock_number,
                    'enter_unit'    => round($info_list_v['unit_cost'],4),
                    'enter_cost'    => (float)$info_list_v['cost_money'],
                    'out_number'  => null,
                    'out_basics'  => null,
                    'out_unit'    => null,
                    'out_cost'    => null,
                    'createtime'    => time(),
                    'updatetime'    => time(),
                ];
                //收发详情结束

                //批次跟踪
                if($batch_set==1 && $goods['batch']==1){
                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    if($goods_attr>0){
                        $where['attribute_id'] = $goods_attr;
                    }
                    $where['stores_id'] = $info_list_v['stores_id'];
                    $where['batch_id'] = $info_list_v['batch_id'];
                    $goods_batch_stock = Db::name('kuyou_goods_batch_stock')->where($where)->find();
                    if(empty($goods_batch_stock)){
                        $status = false;
                        foreach ($goods_batch_add as $goods_batch_add_k=>$goods_batch_add_v){
                            if($goods_batch_add_v['batch_id']==$info_list_v['batch_id'] && $goods_batch_add_v['goods_id']==$info_list_v['goods_id'] && $goods_batch_add_v['attribute_id']==$goods_attr && $goods_batch_add_v['stores_id']==$info_list_v['stores_id']){
                                $goods_batch_add[$goods_batch_add_k]['stock'] += $stock_number;
                                $status = true;
                            }
                        }
                        if(!$status){

                            $goods_attr = isset($info_list_v['goods_attr'])?$info_list_v['goods_attr']:0;
                            $batch_id = Db::name('kuyou_goods_batch')->where(['goods_code'=>$info_list_v['goods_code'],'attribute_id'=>$goods_attr,'stores_id'=>$info_list_v['stores_id']])->value('id');
                            if(empty($batch_id)){
                                $batch_id = Db::name('kuyou_goods_batch')->insertGetId([
                                    'goods_id' => $info_list_v['goods_id'],
                                    'goods_code' => $goods['goods_code'],
                                    'attribute_id' => isset($goods_attr)?$goods_attr:0,
                                    'stores_id' => $info_list_v['stores_id'],
                                    'batch_name' => $info_list_v['batch'],
                                    'manufacture' => $info_list_v['manufacture'],
                                    'guarantee' => $info_list_v['guarantee'],
                                    'validity' => $info_list_v['validity'],
                                    'createtime' => time(),
                                    'updatetime' => time(),
                                ]);
                            }
                            $edit_batch[] = ['id'=>$info_list_v['id'],'batch_id'=>$batch_id];
                            $goods_batch_add[] = [
                                'batch_id' => $batch_id,
                                'goods_id' => $info_list_v['goods_id'],
                                'goods_code' => $goods['goods_code'],
                                'attribute_id' => $goods_attr,
                                'stores_id' => $info_list_v['stores_id'],
                                'stock' => $stock_number,
                                'createtime' => time(),
                                'updatetime' => time(),
                            ];
                        }
                    }else{
                        if(isset($goods_batch_edit[$goods_batch_stock['id']])){
                            $goods_batch_edit[$goods_batch_stock['id']] += $stock_number;
                        }else{
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']+$stock_number;
                        }
                    }
                    //商品批次跟踪
                    $goods_batch_log[] = [
                        'goods_id'      => $goods['id'],
                        'goods_unit'    => $info_list_v['goods_unit'],
                        'stores_id'     => $info_list_v['stores_id'],
                        'batch_id'      => (float)$info_list_v['batch_id'],
                        'number'        => (float)$info_list_v['basic_number'],
                        'purchase_code' => $info_list_v['purchase_code'],
                        'purchase_time' => $info_list_v['purchase_time'],
                        'business_type' => 11,
                        'remarks'       => $info_list_v['remark'],
                        'order_id'      => $info_list_v['id'],
                        'unit_price'    => $info_list_v['unit_cost'],
                        'tax_included'  => $info_list_v['unit_cost'],
                        'all_money'     => $info_list_v['cost_money'],
                        'tax_money'     => 0,
                        'money_tax_price' => $info_list_v['cost_money'],
                        'createtime'    => time(),
                        'updatetime'    => time(),
                    ];
                    //商品批次跟踪结束
                }

                //序列号跟踪
                if($goods && $goods['serial']==1 && $serial_set==1 && !empty($info_list_v['period'])){
                    $period = unserialize($info_list_v['period']);
                    foreach ($period as $period_k=>$period_v){

                        $per_s = Db::name('kuyou_goods_serial')->where(['serial'=>$period_v['serial']])->field('serial,status,store_status')->find();//序列号
                        if($per_s){//仓库有记录
                            $edit_serial[] = ['serial'=>$per_s['serial'],'assemble_id'=>$info_list_v['id'],'type'=>11,'store_status'=>1,'old_status'=>$per_s['status'],'old_store'=>$per_s['store_status']];
                        }else{
                            if(!in_array($period_v['serial'],$per_code)){
                                $per_code[] = $period_v['serial'];
                            }
                            $goods_serial[] = [
                                'assemble_id' => $info_list_v['id'],
                                'goods_id'=>$goods['id'],
                                'goods_code'=>$goods['goods_code'],
                                'stores_id'=>$info_list_v['stores_id'],
                                'attribute_id'=>isset($goods_attr)?$goods_attr:null,
                                'serial'=>$period_v['serial'],
                                'remark'=>$period_v['remark'],
                                'status'=> '11',
                                'store_status'=>'1',
                                'assemble_status'=>'1',
                                'order_id'      => $info_list_v['id'],
                                'createtime'  => time(),
                                'updatetime'  => time(),
                            ];
                        }
                        $serial_log[] = [
                            'goods_id'=>$info_list_v['goods_id'],
                            'stores_id'=>$info_list_v['stores_id'],
                            'attribute_id'=>isset($goods_attr)?$goods_attr:0,
                            'serial'=>$period_v['serial'],
                            'business_type'=>'11',
                            'store_status'=>'1',
                            'remarks' => $period_v['remark'],
                            'purchase_id'=>$info_list_v['id'],
                            'purchase_code'=>$info_list_v['purchase_code'],
                            'purchase_time' => $info_list_v['purchase_time'],
                            'order_id'      => $info_list_v['id'],
                            'createtime'  => time(),
                            'updatetime'  => time(),
                        ];
                    }
                }
                //序列号跟踪
            }else{
                //子件
                //商品表库存
                $status = false;
                foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){
                    if($goods_stock_v['id']==$goods['id']){
                        $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory']-$stock_number;
                        $status = true;
                    }
                }
                if(!$status){
                    $goods_stock[] = [
                        'id'=>$goods['id'],
                        'inventory'=>$goods['inventory']-$stock_number,
                    ];
                }
                foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){
                    if($goods_stock_v['inventory']<0){
                        return ['status'=>500,'msg'=>$goods['goods_name'].'库存不足'];
                    }
                }
                //商品表库存结束

                //商品库存表库存
                $where = [];
                $where['goods_code'] = $info_list_v['goods_code'];
                $where['stores_id'] = $info_list_v['stores_id'];
                $goods_attr = 0;
                if($info_list_v['goods_attr']>0){
                    $goods_attr = Db::name('kuyou_goods_attr')->where(['id'=>$info_list_v['goods_attr']])->value('attribute_id');
                    if($goods_attr>0){
                        $where['attribute_id'] = $goods_attr;
                    }
                }
                $stock = Db::name('kuyou_goods_stock')->where($where)->field('stock,id,cost_money')->find();
                if($stock){
                    $status = false;
                    foreach ($editstock as $editstock_k=>$editstock_v){
                        if($editstock_v['id']==$stock['id']){
                            $editstock[$editstock_k]['stock'] -= $stock_number;
                            $editstock[$editstock_k]['cost_money'] = (round($editstock_v['cost_money']-$info_list_v['cost_money'],4));
                            $status = true;
                        }
                    }
                    if(!$status){
                        $editstock[] = [
                            'id'=>$stock['id'],
                            'stock'=>(round($stock['stock']-$stock_number,4)),
                            'cost_money'=>(round($stock['cost_money']-$info_list_v['cost_money'],4))
                        ];
                    }
                    foreach ($editstock as $editstock_k=>$editstock_v){
                        if($editstock_v['stock']<0){
                            return ['status'=>500,'msg'=>$goods['goods_name'].'库存不足'];
                        }
                    }
                }else{
                    return ['status'=>500,'msg'=>$goods['goods_name'].'库存不足'];
                }
                //商品库存表库存结束

                //收发详情
                $goods_stock_log[] = [
                    'purchase_code' => $info_list_v['purchase_code'],
                    'purchase_id'   => $info_list_v['id'],
                    'purchase_time' => $info_list_v['purchase_time'],
                    'business_type' => '13',
                    'goods_id'      => $goods['id'],
                    'goods_code'    => $goods['goods_code'],
                    'attribute_id'  => (float)$info_list_v['goods_attr'],
                    'goods_unit'    => (float)$info_list_v['goods_unit'],
                    'stores_id'     => (float)$info_list_v['stores_id'],
                    'remark'        => $info_list_v['remark'],
                    'enter_number'  => null,
                    'enter_basics'  => null,
                    'enter_unit'    => null,
                    'enter_cost'    => null,
                    'out_number'  => (float)$info_list_v['number'],
                    'out_basics'  => (float)$stock_number,
                    'out_unit'    => round($info_list_v['unit_cost'],4),
                    'out_cost'    => (float)$info_list_v['cost_money'],
                    'createtime'    => time(),
                    'updatetime'    => time(),
                ];
                //收发详情结束

                //批次跟踪
                if($batch_set==1 && $goods['batch']==1){
                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    if($goods_attr>0){
                        $where['attribute_id'] = $goods_attr;
                    }
                    $where['stores_id'] = $info_list_v['stores_id'];
                    $where['batch_id'] = $info_list_v['batch_id'];
                    $goods_batch_stock = Db::name('kuyou_goods_batch_stock')->where($where)->find();
                    if(empty($goods_batch_stock)){
                        return ['status'=>500,'msg'=>$goods['goods_name'].$info_list_v['batch'].'库存不足'];
                    }else{
                        if(isset($goods_batch_edit[$goods_batch_stock['id']])){
                            $goods_batch_edit[$goods_batch_stock['id']] -= $stock_number;
                        }else{
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']-$stock_number;
                        }
                        foreach ($goods_batch_edit as $goods_batch_edit_k=>$goods_batch_edit_v){
                            if($goods_batch_edit_v<0){
                                return ['status'=>500,'msg'=>$goods['goods_name'].$info_list_v['batch'].'库存不足'];
                            }
                        }
                    }
                    //商品批次跟踪
                    $goods_batch_log[] = [
                        'goods_id'      => $goods['id'],
                        'goods_unit'    => $info_list_v['goods_unit'],
                        'stores_id'     => $info_list_v['stores_id'],
                        'batch_id'      => (float)$info_list_v['batch_id'],
                        'number'        => (float)$info_list_v['basic_number'],
                        'purchase_code' => $info_list_v['purchase_code'],
                        'purchase_time' => $info_list_v['purchase_time'],
                        'business_type' => '12',
                        'remarks'       => $info_list_v['remark'],
                        'order_id'      => $info_list_v['id'],
                        'unit_price'    => $info_list_v['unit_cost'],
                        'tax_included'  => $info_list_v['unit_cost'],
                        'all_money'     => $info_list_v['cost_money'],
                        'tax_money'     => '0',
                        'money_tax_price' => $info_list_v['cost_money'],
                        'createtime'    => time(),
                        'updatetime'    => time(),
                    ];
                    //商品批次跟踪结束
                }

                //序列号跟踪
                if($goods && $goods['serial']==1 && $serial_set==1 && !empty($info_list_v['period'])){
                    $period = unserialize($info_list_v['period']);
                    foreach ($period as $period_k=>$period_v){

                        $per_s = Db::name('kuyou_goods_serial')->where(['serial'=>$period_v['serial']])->field('serial,status,store_status')->find();//序列号
                        if($per_s){//仓库有记录
                            $edit_serial[] = ['serial'=>$per_s['serial'],'assemble_id'=>$info_list_v['id'],'type'=>12,'store_status'=>'2','old_status'=>$per_s['status'],'old_store'=>$per_s['store_status']];
                        }else{
                            return ['status'=>500,'msg'=>$goods['goods_name'].'序列号'.$period_v['serial'].'不存在'];
                        }
                        $serial_log[] = [
                            'goods_id'=>$info_list_v['goods_id'],
                            'stores_id'=>$info_list_v['stores_id'],
                            'attribute_id'=>isset($goods_attr)?$goods_attr:0,
                            'serial'=>$period_v['serial'],
                            'business_type'=>'12',
                            'store_status'=>'2',
                            'remarks' => $period_v['remark'],
                            'purchase_id'=>$info_list_v['id'],
                            'purchase_code'=>$info_list_v['purchase_code'],
                            'purchase_time' => $info_list_v['purchase_time'],
                            'order_id'      => $info_list_v['id'],
                            'createtime'  => time(),
                            'updatetime'  => time(),
                        ];
                    }
                }
                //序列号跟踪
            }
        }

        Db::startTrans();
        try {
            $result = $model->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'0'])->update(['examine_status'=>1,'examine_id'=>$adminId,'examine_time'=>time(),'operation_id'=>$adminId]);
            //库存表修改库存
            if(!empty($editstock)){
                (new GoodsStock())->saveAll($editstock);
            }
            //库存表添加库存
            if(!empty($addstock)){
                Db::name('kuyou_goods_stock')->insertAll($addstock);
            }
            //商品列表库存
            if(!empty($goods_stock)){
                (new Goods())->saveAll($goods_stock);
            }
            //商品收发明细
            if(!empty($goods_stock_log)){
                Db::name('kuyou_goods_log')->insertAll($goods_stock_log);
            }
            //批次库存新增
            if(!empty($goods_batch_add)){
                foreach ($goods_batch_add as $goods_batch_add_k=>$goods_batch_add_v){
                    $where = [];
                    $where['goods_code'] = $goods_batch_add_v['goods_code'];
                    $where['stores_id'] = $goods_batch_add_v['stores_id'];
                    $where['batch_id'] = $goods_batch_add_v['batch_id'];
                    if($goods_batch_add_v['attribute_id']>0){
                        $where['attribute_id'] = $goods_batch_add_v['attribute_id'];
                    }
                    $stock = Db::name('kuyou_goods_batch_stock')->where($where)->find();
                    if($stock){
                        Db::name('kuyou_goods_batch_stock')->where(['id'=>$stock['id']])->inc('stock',$goods_batch_add_v['stock'])->update();
                    }else{
                        Db::name('kuyou_goods_batch_stock')->insert($goods_batch_add_v);
                    }
                }
            }
            //批次库存更新
            if(!empty($goods_batch_edit)){
                foreach ($goods_batch_edit as $k=>$v){
                    Db::name('kuyou_goods_batch_stock')->where(['id'=>$k])->update(['stock'=>$v]);
                }
            }
            //批次记录
            if(!empty($goods_batch_log)){
                Db::name('kuyou_goods_batch_log')->insertAll($goods_batch_log);
            }
            //序列号添加
            if(!empty($goods_serial)){
                Db::name('kuyou_goods_serial')->insertAll($goods_serial);
            }
            //序列号更新
            if(!empty($edit_serial)){
                foreach ($edit_serial as $edit_serial_k=>$edit_serial_v){
                    Db::name('kuyou_goods_serial')->where(['serial'=>$edit_serial_v['serial']])->update([
                        'assemble_id'=>$edit_serial_v['assemble_id'],
                        'status'=>$edit_serial_v['type'],
                        'store_status'=>$edit_serial_v['store_status'],
                        'old_status'=>$edit_serial_v['old_status'],
                        'old_store'=>$edit_serial_v['old_store']
                    ]);
                }
            }
            //序列号跟踪
            if(!empty($serial_log)){
                Db::name('kuyou_goods_serial_log')->insertAll($serial_log);
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return ['status'=>500,'msg'=>$e->getMessage()];
        }
        if(isset($result) && $result !== false){
            return ['status'=>1,'msg'=>sizeof($list)];
        }else{
            return ['status'=>500,'msg'=>'审核失败'];
        }
    }

    /**
     * 反审核
     * @param array $purchase_code
     * @return array
     */
    public static function examinef($purchase_code = null){
        if(empty($purchase_code)){
            return ['status'=>400,'msg'=>'请选择要操作的数据'];
        }
        $adminId = (new Commons())->getAdminId();
        if(empty($adminId)){
            return ['status'=>410,'msg'=>'登陆无效，请重新登陆'];
        }

        $model = new Assemble();
        $list = $model->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'1'])->select();
        if(empty($list)){
            return ['status'=>500,'msg'=>'请选择需要反审核的数据'];
        }
        $serial_set= Db::name('kuyou_sets')->where(['name'=>'serial'])->value('is_checked');//序列号设置
        $batch_set= Db::name('kuyou_sets')->where(['name'=>'batch'])->value('is_checked');//批次设置

        $purchase_code = $model->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'1'])->column('purchase_code');
        $editstock = [];//库存表修改库存
        $goods_stock = [];//商品列表库存
        $goods_batch_edit = [];//批次库存更新
        $info_list = Db::name('kuyou_assemble_info')->where(['purchase_code'=>['in',$purchase_code]])->select();
        foreach ($info_list as $info_list_k=>$info_list_v){
            $goods = Db::name('kuyou_goods')->where(['id'=>$info_list_v['goods_id']])->find();
            $stock_number = $info_list_v['basic_number'];//基础数量
            if($info_list_v['type']==1){
                //组合件
                //商品表库存
                $status = false;
                foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){
                    if($goods_stock_v['id']==$goods['id']){
                        $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory']-$stock_number;
                        $status = true;
                    }
                }
                if(!$status){
                    $goods_stock[] = [
                        'id'=>$goods['id'],
                        'inventory'=>$goods['inventory']-$stock_number,
                    ];
                }
                foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){
                    if($goods_stock_v['inventory']<0){
                        return ['status'=>500,'msg'=>$goods['goods_name'].'库存不足'];
                    }
                }
                //商品表库存结束

                //商品库存表库存
                $where = [];
                $where['goods_code'] = $info_list_v['goods_code'];
                $where['stores_id'] = $info_list_v['stores_id'];
                $goods_attr = 0;
                if($info_list_v['goods_attr']>0){
                    $goods_attr = Db::name('kuyou_goods_attr')->where(['id'=>$info_list_v['goods_attr']])->value('attribute_id');
                    if($goods_attr>0){
                        $where['attribute_id'] = $goods_attr;
                    }
                }
                $stock = Db::name('kuyou_goods_stock')->where($where)->field('stock,id,cost_money')->find();
                if($stock){
                    $status = false;
                    foreach ($editstock as $editstock_k=>$editstock_v){
                        if($editstock_v['id']==$stock['id']){
                            $editstock[$editstock_k]['stock'] -= $stock_number;
                            $editstock[$editstock_k]['cost_money'] = (round($editstock_v['cost_money']-$info_list_v['cost_money'],4));
                            $status = true;
                        }
                    }
                    if(!$status){
                        $editstock[] = [
                            'id'=>$stock['id'],
                            'stock'=>(round($stock['stock']-$stock_number,4)),
                            'cost_money'=>(round($stock['cost_money']-$info_list_v['cost_money'],4))
                        ];
                    }
                    foreach ($editstock as $editstock_k=>$editstock_v){
                        if($editstock_v['stock']<0){
                            return ['status'=>500,'msg'=>$goods['goods_name'].'库存不足'];
                        }
                    }
                }else{
                    return ['status'=>500,'msg'=>$goods['goods_name'].'库存不足'];
                }
                //商品库存表库存结束

                //批次跟踪
                if($batch_set==1 && $goods['batch']==1){
                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    if($goods_attr>0){
                        $where['attribute_id'] = $goods_attr;
                    }
                    $where['stores_id'] = $info_list_v['stores_id'];
                    $where['batch_id'] = $info_list_v['batch_id'];
                    $goods_batch_stock = Db::name('kuyou_goods_batch_stock')->where($where)->find();
                    if(empty($goods_batch_stock)){
                        return ['status'=>500,'msg'=>$goods['goods_name'].$info_list_v['batch'].'库存不足'];
                    }else{
                        if(isset($goods_batch_edit[$goods_batch_stock['id']])){
                            $goods_batch_edit[$goods_batch_stock['id']] -= $stock_number;
                        }else{
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']-$stock_number;
                        }
                        foreach ($goods_batch_edit as $goods_batch_edit_k=>$goods_batch_edit_v){
                            if($goods_batch_edit_v<0){
                                return ['status'=>500,'msg'=>$goods['goods_name'].$info_list_v['batch'].'库存不足'];
                            }
                        }
                    }
                }

                //序列号跟踪
                if($goods && $goods['serial']==1 && $serial_set==1 && !empty($info_list_v['period'])){
                    $period = unserialize($info_list_v['period']);
                    foreach ($period as $period_k=>$period_v){
                        $per_s = Db::name('kuyou_goods_serial')->where(['serial'=>$period_v['serial']])->find();//序列号
                        if($per_s){//仓库有记录
                            $edit_serial[] = ['serial'=>$per_s['serial'],'assemble_id'=>$info_list_v['id'],'type'=>$per_s['old_status'],'store_status'=>$per_s['old_store']];
                        }else{
                            return ['status'=>500,'msg'=>$goods['goods_name'].'序列号'.$period_v['serial'].'不存在'];
                        }
                    }
                }
                //序列号跟踪
            }else{
                //子件
                //商品表库存
                $status = false;
                foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){
                    if($goods_stock_v['id']==$goods['id']){
                        $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory']+$stock_number;
                        $status = true;
                    }
                }
                if(!$status){
                    $goods_stock[] = [
                        'id'=>$goods['id'],
                        'inventory'=>$goods['inventory']+$stock_number,
                    ];
                }
                //商品表库存结束

                //商品库存表库存
                $where = [];
                $where['goods_code'] = $info_list_v['goods_code'];
                $where['stores_id'] = $info_list_v['stores_id'];
                $goods_attr = 0;
                if($info_list_v['goods_attr']>0){
                    $goods_attr = Db::name('kuyou_goods_attr')->where(['id'=>$info_list_v['goods_attr']])->value('attribute_id');
                    if($goods_attr>0){
                        $where['attribute_id'] = $goods_attr;
                    }
                }
                $stock = Db::name('kuyou_goods_stock')->where($where)->field('stock,id,cost_money')->find();
                if($stock){
                    $status = false;
                    foreach ($editstock as $editstock_k=>$editstock_v){
                        if($editstock_v['id']==$stock['id']){
                            $editstock[$editstock_k]['stock'] += $stock_number;
                            $editstock[$editstock_k]['cost_money'] = (round($editstock_v['cost_money']+$info_list_v['cost_money'],4));
                            $status = true;
                        }
                    }
                    if(!$status){
                        $editstock[] = [
                            'id'=>$stock['id'],
                            'stock'=>(round($stock['stock']+$stock_number,4)),
                            'cost_money'=>(round($stock['cost_money']+$info_list_v['cost_money'],4))
                        ];
                    }
                }
                //商品库存表库存结束

                //批次跟踪
                if($batch_set==1 && $goods['batch']==1){
                    $where = [];
                    $where['goods_id'] = $goods['id'];
                    if($goods_attr>0){
                        $where['attribute_id'] = $goods_attr;
                    }
                    $where['stores_id'] = $info_list_v['stores_id'];
                    $where['batch_id'] = $info_list_v['batch_id'];
                    $goods_batch_stock = Db::name('kuyou_goods_batch_stock')->where($where)->find();
                    if(!empty($goods_batch_stock)){
                        if(isset($goods_batch_edit[$goods_batch_stock['id']])){
                            $goods_batch_edit[$goods_batch_stock['id']] += $stock_number;
                        }else{
                            $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']+$stock_number;
                        }
                    }
                }

                //序列号跟踪
                if($goods && $goods['serial']==1 && $serial_set==1 && !empty($info_list_v['period'])){
                    $period = unserialize($info_list_v['period']);
                    foreach ($period as $period_k=>$period_v){
                        $per_s = Db::name('kuyou_goods_serial')->where(['serial'=>$period_v['serial']])->find();//序列号
                        if($per_s){//仓库有记录
                            $edit_serial[] = ['serial'=>$per_s['serial'],'assemble_id'=>$info_list_v['id'],'type'=>$per_s['old_status'],'store_status'=>$per_s['old_store']];
                        }else{
                            return ['status'=>500,'msg'=>$goods['goods_name'].'序列号'.$period_v['serial'].'不存在'];
                        }
                    }
                }
                //序列号跟踪
            }
        }

        $info_id = Db::name('kuyou_assemble_info')->where(['purchase_code'=>['in',$purchase_code]])->column('id');

        Db::startTrans();
        try {
            $result = $model->where(['purchase_code'=>['in',$purchase_code],'examine_status'=>'1'])->update(['examine_status'=>0,'examine_id'=>0,'examine_time'=>'','operation_id'=>$adminId]);
            if(!empty($editstock)){
                (new GoodsStock())->saveAll($editstock);
            }
            if(!empty($goods_stock)){
                (new Goods())->saveAll($goods_stock);
            }
            if(!empty($goods_batch_edit)){
                foreach ($goods_batch_edit as $k=>$v){
                    Db::name('kuyou_goods_batch_stock')->where(['id'=>$k])->update(['stock'=>$v]);
                }
            }

            Db::name('kuyou_goods_log')->where(['business_type'=>['in',[12,13]],'purchase_id'=>['in',$info_id]])->delete();

            Db::name('kuyou_goods_batch_log')->where(['business_type'=>['in',[11,12]],'order_id'=>['in',$info_id]])->delete();

            Db::name('kuyou_goods_serial')->where(['assemble_id'=>['in',$info_id],'assemble_status'=>'1'])->delete();
            $serial = Db::name('kuyou_goods_serial')->where(['assemble_id'=>['in',$info_id],'assemble_status'=>'2'])->select();
            foreach ($serial as $serial_k=>$serial_v){
                Db::name('kuyou_goods_serial')->where(['id'=>$serial_v['id']])->update(['status'=>$serial_v['old_status'],'store_status'=>$serial_v['old_store']]);
            }
            Db::name('kuyou_goods_serial_log')->where(['business_type'=>['in',[11,12]],'order_id'=>['in',$info_id]])->delete();
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return ['status'=>500,'msg'=>$e->getMessage()];
        }
        if(isset($result) && $result !== false){
            return ['status'=>1,'msg'=>sizeof($list)];
        }else{
            return ['status'=>500,'msg'=>'反审核失败'];
        }
    }

    /**
     * 添加
     * @param array $detail
     * @return array
     */
    public static function addTemplate($detail){
        if(empty($detail)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $check = (new Commons())->checkParameter($detail,[
                'purchase_code|单据编号'=>'require',
                'purchase_time|单据时间'=>'require',
                'temp_name|模板名称'=>'require',
                'costs|组装费用'=>'requireNone|money',
                'plist|组合件信息'=>'require',
                'zlist|子件信息'=>'require',
            ]);
            if(isset($check['status'])){
                return $check;
            }else{
                $adminId = (new Commons())->getAdminId();
                if(empty($adminId)){
                    return ['status'=>410,'msg'=>'登陆无效，请重新登陆'];
                }
                $status = (new Commons())->checkCode($detail['purchase_code'],19);//验证编号是否合法
                if($status){
                    $team_name = isset($detail['temp_name'])?trim($detail['temp_name']):'';
                    $team = Db::name('kuyou_assemble_temp')->where(['temp_name'=>$team_name])->find();
                    if($team){
                        return ['status'=>500,'msg'=>'模板名称不能重复'];
                    }

                    $serial_set= Db::name('kuyou_sets')->where(['name'=>'serial'])->value('is_checked');//序列号设置
                    $batch_set= Db::name('kuyou_sets')->where(['name'=>'batch'])->value('is_checked');//批次设置

                    $plist = (array)json_decode($detail['plist'], true);
                    $zlist = (array)json_decode($detail['zlist'], true);
                    foreach ($zlist as $zlist_k=>$zlist_v){
                        $plist[] = $zlist_v;
                    }

                    $data = [];
                    $goods_serial_all = [];//全部序列号,重复判断使用
                    $p = 2;$z = 2;
                    foreach ($plist as $k=>$v){
                        if(!empty($v['goods_code'])){//有产品编号
                            if(!isset($v['goods_unit']) || empty($v['goods_unit'])){
                                return ['status'=>500,'msg'=>'请选择单位'];
                            }
                            if(!isset($v['stores_id']) || empty($v['stores_id'])){
                                return ['status'=>500,'msg'=>'请选择仓库'];
                            }

                            $goods = Db::name('kuyou_goods')->where(['goods_code'=>$v['goods_code']])->find();
                            $goods_unit = $v['goods_unit']===''?null:(float)$v['goods_unit'];
                            $advance_rate = 1;//换算进率
                            if($goods['more_unit']==1) {
                                if($goods_unit!=$goods['unit_id']){
                                    //所选单位不等于商品默认单位，需查询进率换算基础数量
                                    $more_unit = Db::name('kuyou_unit_conversion')
                                        ->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$goods_unit])
                                        
                                        ->field('id,advance_rate')
                                        ->find();
                                    if(empty($more_unit)){
                                        return ['status'=>500,'msg'=>'商品单位信息查询失败'];
                                    }else{
                                        $advance_rate = $more_unit['advance_rate'];
                                    }
                                }
                            }
                            $basic_number = round(($advance_rate*$v['number']),4);

                            if($k==0){
                                //组合件
                                //批次
                                $batch_data = Db::name('kuyou_goods_batch')->where(['id'=>$v['batch_id']])->find();//已有批次
                                if($goods['batch']==1 && $batch_set==1){
                                    if(empty($v['batch_name'])){
                                        if($batch_data){
                                            return ['status'=>500,'msg'=>'请选择生产批次'];
                                        }else{
                                            return ['status'=>500,'msg'=>'请输入生产批次'];
                                        }
                                    }
                                    if(empty($v['manufacture'])){
                                        return ['status'=>500,'msg'=>'请输入生产日期'];
                                    }
                                    if(empty($v['guarantee'])){
                                        return ['status'=>500,'msg'=>'请输入保质期'];
                                    }
                                    if(empty($v['validity'])){
                                        return ['status'=>500,'msg'=>'请输入过期时间'];
                                    }
                                }
                                //序列号
                                $goods_serial='';
                                if(isset($v['period_period']) && $goods['serial']==1 && $serial_set==1){
                                    if(empty($v['period_period'])){
                                        return ['status'=>500,'msg'=>'请输入商品序列号'];
                                    }
                                    $goods_serial = serialize($v['period_period']);
                                    $period_period = $v['period_period'];
                                    if(!is_array($period_period)){
                                        $period_period = (array)json_decode($period_period, true);
                                    }
                                    $serial_code = [];
                                    foreach ($period_period as $period_period_k=>$period_period_v){
                                        if(in_array($period_period_v['serial'],$goods_serial_all)){
                                            return ['status'=>500,'msg'=>'商品序列号'.$period_period_v['serial'].'重复'];
                                        }else{
                                            $goods_serial_all[] = $period_period_v['serial'];
                                        }
                                        $serial_code[] = $period_period_v['serial'];
                                    }
                                    $serial = Db::name('kuyou_goods_serial')->where(['serial'=>['in',$serial_code]])->column('serial');
                                    if($serial){
                                        return ['status'=>500,'msg'=>'商品序列号'.$serial[0].'已占用'];
                                    }
                                    $basic_number = (float)$v['number'];
                                }

                                $data[] = [
                                    'purchase_code'     =>  $detail['purchase_code'],
                                    'purchase_time'     =>  strtotime($detail['purchase_time']),
                                    'business_type'     =>  1,
                                    'type'              =>  1,

                                    'goods_id'          =>  $goods['id'],//商品信息
                                    'goods_code'        =>  $goods['goods_code'],
                                    'goods_attr'        =>  $v['goods_attr']===''?null:(float)$v['goods_attr'],
                                    'goods_unit'        =>  $v['goods_unit']===''?null:(float)$v['goods_unit'],

                                    'stores_id'         =>  $v['stores_id']===''?null:(float)$v['stores_id'],
                                    'allow_stock'       =>  $v['allow_stock']===''?null:(float)$v['allow_stock'],
                                    'number'            =>  $v['number']===''?null:(float)$v['number'],
                                    'period'            =>  $goods_serial,
                                    'basic_number'      =>  $basic_number,
                                    'unit_cost'         =>  $v['unit_cost']===''?null:(float)$v['unit_cost'],
                                    'cost_money'        =>  $v['cost_money']===''?null:(float)$v['cost_money'],
                                    'remark'            =>  $v['remark']===''?null:$v['remark'],

                                    'batch'             =>  $v['batch_name']===''?null:$v['batch_name'],
                                    'batch_id'          =>  $v['batch_id']===''?null:(float)$v['batch_id'],
                                    'manufacture'       =>  $v['manufacture']===''?null:strtotime($v['manufacture']),
                                    'guarantee'         =>  $v['guarantee']===''?null:(float)$v['guarantee'],
                                    'validity'          =>  $v['validity']===''?null:strtotime($v['validity']),

                                    'preparation_id'    =>  $adminId,
                                    'operation_id'      =>  $adminId,

                                    'createtime'        => time(),
                                    'updatetime'        => time(),
                                ];
                                $p = 1;
                            }else{
                                //子件
                                if($v==$plist[0]['goods_code']){
                                    return ['status'=>500,'msg'=>'子件不能和组合件为同一商品'];
                                }
                                //批次
                                $batch_data = Db::name('kuyou_goods_batch')->where(['id'=>$v['batch_id']])->find();//已有批次
                                if($goods['batch']==1 && $batch_set==1){
                                    if(empty($v['batch_name'])){
                                        return ['status'=>500,'msg'=>'请选择生产批次'];
                                    }
                                    if(empty($v['manufacture'])){
                                        return ['status'=>500,'msg'=>'请输入生产日期'];
                                    }
                                    if(empty($v['guarantee'])){
                                        return ['status'=>500,'msg'=>'请输入保质期'];
                                    }
                                    if(empty($v['validity'])){
                                        return ['status'=>500,'msg'=>'请输入过期时间'];
                                    }
                                    if(empty($batch_data)){
                                        return ['status'=>500,'msg'=>'商品批次信息查询失败'];
                                    }
                                }
                                //序列号
                                $goods_serial='';
                                if(isset($v['period_period']) && $goods['serial']==1 && $serial_set==1){
                                    if(empty($v['period_period'])){
                                        return ['status'=>500,'msg'=>'请输入商品序列号'];
                                    }
                                    $goods_serial = serialize($v['period_period']);
                                    $period_period = $v['period_period'];
                                    if(!is_array($period_period)){
                                        $period_period = (array)json_decode($period_period, true);
                                    }
                                    $serial_code = [];
                                    foreach ($period_period as $period_period_k=>$period_period_v){
                                        if(in_array($period_period_v['serial'],$goods_serial_all)){
                                            return ['status'=>500,'msg'=>'商品序列号'.$period_period_v['serial'].'重复'];
                                        }else{
                                            $goods_serial_all[] = $period_period_v['serial'];
                                        }
                                        $serial_code[] = $period_period_v['serial'];
                                    }
                                    $serial = Db::name('kuyou_goods_serial')->where(['serial'=>['in',$serial_code],'store_status'=>'1'])->column('serial');
                                    if(sizeof($serial)!=sizeof($serial_code)){
                                        foreach ($serial_code as $serial_code_k=>$serial_code_v){
                                            if(in_array($serial_code_v,$serial)){
                                                return ['status'=>500,'msg'=>'商品序列号'.$serial_code_v.'不在库，不可出库'];
                                            }
                                        }
                                        return ['status'=>500,'msg'=>'商品序列号不在库，不可出库'];
                                    }
                                    $basic_number = (float)$v['number'];
                                }

                                $data[] = [
                                    'purchase_code'     =>  $detail['purchase_code'],
                                    'purchase_time'     =>  strtotime($detail['purchase_time']),
                                    'business_type'     =>  1,
                                    'type'              =>  2,

                                    'goods_id'          =>  $goods['id'],//商品信息
                                    'goods_code'        =>  $goods['goods_code'],
                                    'goods_attr'        =>  $v['goods_attr']===''?null:(float)$v['goods_attr'],
                                    'goods_unit'        =>  $v['goods_unit']===''?null:(float)$v['goods_unit'],

                                    'stores_id'         =>  $v['stores_id']===''?null:(float)$v['stores_id'],
                                    'allow_stock'       =>  $v['allow_stock']===''?null:(float)$v['allow_stock'],
                                    'number'            =>  $v['number']===''?null:(float)$v['number'],
                                    'period'            =>  $goods_serial,
                                    'basic_number'      =>  $basic_number,
                                    'unit_cost'         =>  $v['unit_cost']===''?null:(float)$v['unit_cost'],
                                    'cost_money'        =>  $v['cost_money']===''?null:(float)$v['cost_money'],
                                    'remark'            =>  $v['remark']===''?null:$v['remark'],

                                    'batch'             =>  $v['batch_name']===''?null:$v['batch_name'],
                                    'batch_id'          =>  $v['batch_id']===''?null:(float)$v['batch_id'],
                                    'manufacture'       =>  $v['manufacture']===''?null:strtotime($v['manufacture']),
                                    'guarantee'         =>  $v['guarantee']===''?null:(float)$v['guarantee'],
                                    'validity'          =>  $v['validity']===''?null:strtotime($v['validity']),

                                    'preparation_id'    =>  $adminId,
                                    'operation_id'      =>  $adminId,

                                    'createtime'        => time(),
                                    'updatetime'        => time(),
                                ];
                                $z = 1;
                            }
                        }
                    }
                    if($p==2){
                        return ['status'=>500,'msg'=>'组合件信息不能为空'];
                    }
                    if($z==2){
                        return ['status'=>500,'msg'=>'组合子件信息不能为空'];
                    }
                    if(!empty($data)){
                        $rowdata = [
                            'temp_name'         =>  $team_name,
                            'purchase_code'     =>  $detail['purchase_code'],
                            'purchase_time'     =>  strtotime($detail['purchase_time']),
                            'business_type'     =>  1,
                            'money'             =>  $detail['costs']===''?null:(float)$detail['costs'],
                            'remark'            =>  $detail['note']===''?null:$detail['note'],
                            'preparation_id'    =>  $adminId,
                            'operation_id'      =>  $adminId,
                            'createtime'        => time(),
                            'updatetime'        => time(),
                        ];
                    }else{
                        return ['status'=>500,'msg'=>'组件信息不能为空'];
                    }

                    $model = new AssembleTemp();
                    $result = false;
                    Db::startTrans();
                    try {
                        $model->allowField(true)->save($rowdata);
                        $result = $model->id;
                        foreach ($data as $data_k=>$data_v){
                            $data[$data_k]['purchase_id'] = $result;
                        }
                        Db::name('kuyou_assemble_info_temp')->insertAll($data);
                        Db::commit();
                    } catch (Exception $e) {
                        Db::rollback();
                        return ['status'=>500,'msg'=>$e->getMessage()];
                    }
                    if($result){
                        return ['status'=>200,'msg'=>$result];
                    }else{
                        return ['status'=>500,'msg'=>'添加失败'];
                    }
                }else{
                    return ['status'=>500,'msg'=>'编号规则错误'];
                }
            }
        }
    }

    /**
     * 获取模板列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @return array
     */
    public static function getTemplateList($where,$page,$limit){
        $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
        $pageStatus = true;
        if($app_page){
            if($page<0){
                $pageStatus = false;
            }
        }

        $model = new AssembleTemp();
        if($pageStatus){
            $list = $model->where($where)->page($page,$limit)->field("id,temp_name,purchase_code,FROM_UNIXTIME(purchase_time,'%Y-%m-%d') as purchase_time,
            business_type,money,remark,preparation_id,examine_id,examine_status,print_num")->select();
        }else{
            $list = $model->where($where)->field("id,temp_name,purchase_code,FROM_UNIXTIME(purchase_time,'%Y-%m-%d') as purchase_time,
            business_type,money,remark,preparation_id,examine_id,examine_status,print_num")->select();
        }

        $lists = [];
        foreach ($list as $list_k=>$list_v){
            $p_info = Db::name('kuyou_assemble_info_temp')->alias('a')
                ->join('kuyou_goods g','g.id=a.goods_id')
                ->where(['a.purchase_id'=>$list_v['id'],'a.type'=>'1'])
                ->field('a.number,g.goods_name,g.goods_specification_id as spec')->find();
            $info = Db::name('kuyou_assemble_info_temp')->alias('a')
                ->join('kuyou_goods g','g.id=a.goods_id')
                ->where(['a.purchase_id'=>$list_v['id'],'a.type'=>'2'])
                ->field('a.number,g.goods_name,g.goods_specification_id as spec')
                ->select();
            $row = [];
            $row['id'] = $list_v['id'];
            $row['temp_name'] = $list_v['temp_name'];
            $row['pnumber'] = $p_info['number'];
            $row['pname'] = $p_info['goods_name'].'_'.$p_info['spec'];
            $row['z_list'] = [];
            foreach ($info as $info_k=>$info_v){
                $zrow = [];
                $zrow['number'] = $info_v['number'];
                $zrow['name'] = $info_v['goods_name'].'_'.$info_v['spec'];
                $row['z_list'][] = $zrow;
            }
            $lists[] = $row;
        }

        $count = $model->where($where)->count();
        return ['list'=>$lists,'count'=>$count];
    }

    /**
     * 删除模板
     * @param string $id
     * @return array
     */
    public static function delTemplate($id){
        if(empty($id)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $id = explode(',',$id);
            $model = new AssembleTemp();

            $list = $model->where(['id'=>['in',$id]])->select();
            $count = 0;
            $res = false;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    $res = $model->where(['id'=>$v['id']])->delete();
                    $count += $res;
                    Db::name('kuyou_assemble_info_temp')->where(['purchase_id'=>$v['id']])->delete();
                }
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                return ['status'=>500,'msg'=>$e->getMessage()];
            }
            if($res){
                return ['status'=>200,'msg'=>$count];
            }else{
                return ['status'=>500,'msg'=>'删除失败'];
            }
        }
    }

    /**
     * 获取详情
     * @param string $purchase_code
     * @return array
     */
    public static function getTemplateDetail($id){
        $model = new AssembleTemp();
        $detail = $model->where(['id'=>$id])->field("id,purchase_code,FROM_UNIXTIME(purchase_time,'%Y-%m-%d') as purchase_time,
            business_type,money,remark,preparation_id,examine_id,examine_status,print_num")->find();
        if($detail){
            $list = Db::name('kuyou_assemble_info_temp')->alias('a')
                ->join('kuyou_goods g','g.id=a.goods_id')
                ->where(['a.purchase_id'=>$detail['id'],'type'=>'1'])
                ->field('a.type,a.goods_id,a.goods_code,a.goods_attr,a.goods_unit,a.stores_id,a.allow_stock,a.number,
                a.period,a.basic_number,a.unit_cost,a.cost_money,a.remark,a.batch_id,a.batch,a.guarantee,
                FROM_UNIXTIME(manufacture,\'%Y-%m-%d\') as manufacture,FROM_UNIXTIME(validity,\'%Y-%m-%d\') as validity,
                g.id as goods_id,g.auxiliary,g.goods_name,g.goods_image,g.batch as batch_status,g.serial')
                ->select();
            foreach ($list as $list_k=>$list_v){
                if(!empty($list_v['period'])){
                    $list[$list_k]['period'] = unserialize($list_v['period']);
                }

                $field = 'id,goods_code,goods_name,batch as batch_status,serial as serial_status,unit_id,more_unit,more_unit_id,purchase,goods_image,inventory,note,auxiliary,manager_warehouse_id,shelf_life';
                $row = Db::name('kuyou_goods')->where(['id'=>$list_v['goods_id']])->field($field)->find();
                $goods_id = $row['id'];
                $auxiliary = Db::name('kuyou_auxiliary')->where(['status'=>1])->column('name','id');

                $field = 'id,attribute_id,purchase,discount,discount2';
                $goods_attr = Db::name('kuyou_goods_attr')->where(['goods_id'=>$goods_id])->field($field)->select();
                foreach ($goods_attr as $goods_attr_k=>$goods_attr_v){
                    $aux_combination = Db::name('kuyou_aux_combination')->where(['id'=>$goods_attr_v['attribute_id']])->value('attr_ids');
                    $goods_attr[$goods_attr_k]['attr_ids'] = [];
                    $goods_attr[$goods_attr_k]['attr_name'] = '';
                    if($aux_combination){
                        $goods_attr[$goods_attr_k]['attr_id'] = $aux_combination;
                        $aux_combination = explode('&',$aux_combination);
                        $goods_attr[$goods_attr_k]['attr_ids'] = $aux_combination;
                        foreach ($aux_combination as $aux_combination_k=>$aux_combination_v){
                            if(isset($auxiliary[$aux_combination_v])){
                                $aux_combination[$aux_combination_k] = $auxiliary[$aux_combination_v];
                            }
                        }
                        $aux_combination = implode('/',$aux_combination);
                        $goods_attr[$goods_attr_k]['attr_name'] = $aux_combination;
                    }
                }

                $field = 'id,unit,is_default,purchase,discount,discount2';
                if($row['more_unit']==1){
                    $unit = Db::name('kuyou_unit_conversion')->alias('uc')
                        ->join('kuyou_unit u','u.id=uc.vice_unit')
                        ->where(['uc.main_unit'=>$row['unit_id']])
                        ->order('uc.advance_rate','ASC')
                        ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')
                        ->select();
                    $unit_ids = [$row['unit_id']];
                    foreach ($unit as $unit_k=>$unit_v){
                        $unit_ids[] = $unit_v['id'];
                    }
                    $goods_price = Db::name('kuyou_goods_price')->where(['unit'=>['in',$unit_ids],'goods_id'=>$goods_id])->field($field)->select();
                    foreach ($goods_price as $goods_price_k=>$goods_price_v){
                        foreach ($unit as $unit_k=>$unit_v){
                            if($goods_price_v['unit']==$row['unit_id']){
                                $goods_price[$goods_price_k]['advance_rate'] = 1;
                            }else if($goods_price_v['unit']==$unit_v['id']){
                                $goods_price[$goods_price_k]['advance_rate'] = $unit_v['advance_rate'];
                            }
                        }
                    }
                }else{
                    $unit_ids = [$row['unit_id']];
                }
                $unit = Db::name('kuyou_unit')->where(['id'=>['in',$unit_ids]])->field('id,name')->select();
                foreach ($unit as $unit_k=>$unit_v){
                    if(isset($goods_price)){
                        foreach ($goods_price as $goods_price_k=>$goods_price_v){
                            if($unit_v['id']==$goods_price_v['unit']){
                                $unit[$unit_k]['is_default'] = $goods_price_v['is_default'];
                                //$unit[$unit_k]['purchase'] = $goods_price_v['purchase']*$goods_price_v['advance_rate'];
                                $unit[$unit_k]['purchase'] = $goods_price_v['purchase'];
                                if($goods_price_v['is_default']!=1){
                                    $unit[$unit_k]['basic_number'] = $goods_price_v['advance_rate'];
                                }else{
                                    $unit[$unit_k]['basic_number'] = 1;
                                }
                            }
                        }
                    }else{
                        if($row['auxiliary']==1 && !empty($attr)){
                            $purchase = Db::name('kuyou_goods_attr')->where(['goods_id'=>$goods_id,'id'=>$attr])->value('purchase');
                        }else{
                            $purchase = $row['purchase'];
                        }
                        if($unit_v['id']==$row['unit_id']){
                            $unit[$unit_k]['is_default'] = 1;
                            $unit[$unit_k]['basic_number'] = 1;
                            $unit[$unit_k]['purchase'] = $purchase;
                        }
                    }
                }

                foreach ($unit as $unit_k=>$unit_v){
                    $unit[$unit_k]['purchase'] = $unit_v['purchase'];
                    $unit[$unit_k]['tax_included'] = $unit_v['purchase'];
                }
                $data = [];
                foreach ($unit_ids as $unit_ids_k=>$unit_ids_v){
                    foreach ($unit as $unit_k=>$unit_v){
                        if($unit_ids_v==$unit_v['id']){
                            $data[] = $unit_v;
                        }
                    }
                }

                $list[$list_k]['price_data'] = $data;
                $list[$list_k]['attr_data'] = $goods_attr;
            }
            $detail['p_list'] = $list;
            $zlist = Db::name('kuyou_assemble_info_temp')->alias('a')
                ->join('kuyou_goods g','g.id=a.goods_id')
                ->where(['a.purchase_id'=>$detail['id'],'type'=>'2'])
                ->field('a.type,a.goods_id,a.goods_code,a.goods_attr,a.goods_unit,a.stores_id,a.allow_stock,a.number,
                a.period,a.basic_number,a.unit_cost,a.cost_money,a.remark,a.batch_id,a.batch,a.guarantee,
                FROM_UNIXTIME(manufacture,\'%Y-%m-%d\') as manufacture,FROM_UNIXTIME(validity,\'%Y-%m-%d\') as validity,
                g.id as goods_id,g.auxiliary,g.goods_name,g.goods_image,g.batch as batch_status,g.serial')
                ->select();
            foreach ($zlist as $zlist_k=>$zlist_v){
                if(!empty($zlist_v['period'])){
                    $zlist[$zlist_k]['period'] = unserialize($zlist_v['period']);
                }

                $field = 'id,goods_code,goods_name,batch as batch_status,serial as serial_status,unit_id,more_unit,more_unit_id,purchase,goods_image,inventory,note,auxiliary,manager_warehouse_id,shelf_life';
                $row = Db::name('kuyou_goods')->where(['id'=>$list_v['goods_id']])->field($field)->find();
                $goods_id = $row['id'];
                $auxiliary = Db::name('kuyou_auxiliary')->where(['status'=>1])->column('name','id');

                $field = 'id,attribute_id,purchase,discount,discount2';
                $goods_attr = Db::name('kuyou_goods_attr')->where(['goods_id'=>$goods_id])->field($field)->select();
                foreach ($goods_attr as $goods_attr_k=>$goods_attr_v){
                    $aux_combination = Db::name('kuyou_aux_combination')->where(['id'=>$goods_attr_v['attribute_id']])->value('attr_ids');
                    $goods_attr[$goods_attr_k]['attr_ids'] = [];
                    $goods_attr[$goods_attr_k]['attr_name'] = '';
                    if($aux_combination){
                        $goods_attr[$goods_attr_k]['attr_id'] = $aux_combination;
                        $aux_combination = explode('&',$aux_combination);
                        $goods_attr[$goods_attr_k]['attr_ids'] = $aux_combination;
                        foreach ($aux_combination as $aux_combination_k=>$aux_combination_v){
                            if(isset($auxiliary[$aux_combination_v])){
                                $aux_combination[$aux_combination_k] = $auxiliary[$aux_combination_v];
                            }
                        }
                        $aux_combination = implode('/',$aux_combination);
                        $goods_attr[$goods_attr_k]['attr_name'] = $aux_combination;
                    }
                }

                $field = 'id,unit,is_default,purchase,discount,discount2';
                if($row['more_unit']==1){
                    $unit = Db::name('kuyou_unit_conversion')->alias('uc')
                        ->join('kuyou_unit u','u.id=uc.vice_unit')
                        ->where(['uc.main_unit'=>$row['unit_id']])
                        ->order('uc.advance_rate','ASC')
                        ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')
                        ->select();
                    $unit_ids = [$row['unit_id']];
                    foreach ($unit as $unit_k=>$unit_v){
                        $unit_ids[] = $unit_v['id'];
                    }
                    $goods_price = Db::name('kuyou_goods_price')->where(['unit'=>['in',$unit_ids],'goods_id'=>$goods_id])->field($field)->select();
                    foreach ($goods_price as $goods_price_k=>$goods_price_v){
                        foreach ($unit as $unit_k=>$unit_v){
                            if($goods_price_v['unit']==$row['unit_id']){
                                $goods_price[$goods_price_k]['advance_rate'] = 1;
                            }else if($goods_price_v['unit']==$unit_v['id']){
                                $goods_price[$goods_price_k]['advance_rate'] = $unit_v['advance_rate'];
                            }
                        }
                    }
                }else{
                    $unit_ids = [$row['unit_id']];
                }
                $unit = Db::name('kuyou_unit')->where(['id'=>['in',$unit_ids]])->field('id,name')->select();
                foreach ($unit as $unit_k=>$unit_v){
                    if(isset($goods_price)){
                        foreach ($goods_price as $goods_price_k=>$goods_price_v){
                            if($unit_v['id']==$goods_price_v['unit']){
                                $unit[$unit_k]['is_default'] = $goods_price_v['is_default'];
                                //$unit[$unit_k]['purchase'] = $goods_price_v['purchase']*$goods_price_v['advance_rate'];
                                $unit[$unit_k]['purchase'] = $goods_price_v['purchase'];
                                if($goods_price_v['is_default']!=1){
                                    $unit[$unit_k]['basic_number'] = $goods_price_v['advance_rate'];
                                }else{
                                    $unit[$unit_k]['basic_number'] = 1;
                                }
                            }
                        }
                    }else{
                        if($row['auxiliary']==1 && !empty($attr)){
                            $purchase = Db::name('kuyou_goods_attr')->where(['goods_id'=>$goods_id,'id'=>$attr])->value('purchase');
                        }else{
                            $purchase = $row['purchase'];
                        }
                        if($unit_v['id']==$row['unit_id']){
                            $unit[$unit_k]['is_default'] = 1;
                            $unit[$unit_k]['basic_number'] = 1;
                            $unit[$unit_k]['purchase'] = $purchase;
                        }
                    }
                }

                foreach ($unit as $unit_k=>$unit_v){
                    $unit[$unit_k]['purchase'] = $unit_v['purchase'];
                    $unit[$unit_k]['tax_included'] = $unit_v['purchase'];
                }
                $data = [];
                foreach ($unit_ids as $unit_ids_k=>$unit_ids_v){
                    foreach ($unit as $unit_k=>$unit_v){
                        if($unit_ids_v==$unit_v['id']){
                            $data[] = $unit_v;
                        }
                    }
                }

                $zlist[$zlist_k]['price_data'] = $data;
                $zlist[$zlist_k]['attr_data'] = $goods_attr;
            }
            $detail['z_list'] = $zlist;
        }
        if(!empty($detail)){
            if(!empty($detail['cost_data'])){
                $detail['cost_data'] = (array)json_decode($detail['cost_data'], true);
            }
            return ['status'=>200,'msg'=>$detail];
        }else{
            return ['status'=>500,'msg'=>'未查询到相关数据'];
        }
    }
}
